diff --git a/phoenix-routing.md b/phoenix-routing.md index 8d3ac4e95..84b09eb03 100644 --- a/phoenix-routing.md +++ b/phoenix-routing.md @@ -1,13 +1,20 @@ --- title: "Phoenix: Routing" category: Elixir +layout: 2017/sheet +weight: -1 --- +### Showing routes + ```sh -mix phoenix.routes +mix phx.routes # 1.3+ +mix phoenix.routes # 1.2 and below ``` -## Single routes +See: [Mix.Tasks.Phoenix.Routes](https://hexdocs.pm/phoenix/Mix.Tasks.Phoenix.Routes.html) _(hexdocs.pm)_ + +### Single routes ```elixir get "/", PageController, :index @@ -15,19 +22,23 @@ get "/", PageController, :index Also: `put` `post` `patch` `options` `delete` `head` -## Resources +### Resources ```elixir resources "/users", UserController resources "/users", UserController, only: [:index, :show] resources "/users", UserController, except: [:delete] +``` +```elixir +resources "/users", UserController, as: :person # helper name (person_path) name: :person # ...? param: :id # name of parameter for this resource - ``` +Generates these routes: + | Method | Path | Helper | | ---- | ---- | ---- | | GET | `/users` | `user_path(:index)` | @@ -37,34 +48,45 @@ resources "/users", UserController, except: [:delete] | POST | `/users` | `user_path(:create, user)` | | PATCH/PUT | `/users/:id` | `user_path(:update, user)` | | DELETE | `/users/:id` | `user_path(:delete, user)` | +{: .-left-align} -## Path helpers +See: [resources/4](https://hexdocs.pm/phoenix/Phoenix.Router.html#resources/4) _(hexdocs.pm)_ + +### Path helpers ```elixir -user_path(Endpoint, :index) #=> /users -user_path(Endpoint, :show, 17) #=> /users/17 -user_path(Endpoint, :show, %User{id: 17}) #=> /users/17 -user_path(Endpoint, :show, 17, admin: true) #=> /users/17?admin=true +user_path(conn, :index) # → /users +user_path(conn, :show, 17) # → /users/17 +user_path(conn, :show, %User{id: 17}) # → /users/17 +user_path(conn, :show, 17, admin: true) # → /users/17?admin=true +``` -user_url(Endpoint, :index) #=> "http://localhost:4000/users" +```elixir +user_url(conn, :index) # → "http://localhost:4000/users" ``` ```elixir MyApp.Router.Helpers.user_path(MyApp.Endpoint, :index) ``` -## Nested resources +See: [Helpers](https://hexdocs.pm/phoenix/Phoenix.Router.html#module-helpers) _(hexdocs.pm)_ + +### Nested resources ```elixir resources "/users", UserController do resources "/posts", PostController end - -user_post_path(:index, 17) #=> /users/17/posts -user_post_path(:show, 17, 12) #=> /users/17/posts/12 ``` -## Scoped routes +```elixir +user_post_path(:index, 17) # → /users/17/posts +user_post_path(:show, 17, 12) # → /users/17/posts/12 +``` + +See: [Scopes and resources](https://hexdocs.pm/phoenix/Phoenix.Router.html#module-scopes-and-resources) _(hexdocs.pm)_ + +### Scoped routes ```elixir scope "/admin" do @@ -78,3 +100,5 @@ end scope "/admin", as: :admin do: ... end # admin_reviews_path() -> /admin/reviews ``` + +See: [scope/2](https://hexdocs.pm/phoenix/Phoenix.Router.html#scope/2) _(hexdocs.pm)_ diff --git a/phoenix.md b/phoenix.md index 4ad9a9db2..918972378 100644 --- a/phoenix.md +++ b/phoenix.md @@ -9,16 +9,32 @@ updated: 2017-09-04 ### Directory structure ``` -config/ -web/ - controllers/ - models/ - views/ - templates/ - static/ +./ +├── _build +├── assets/ +│ ├── css/ +│ ├── js/ +│ ├── static/ +│ └── node_modules/ +├── config/ +├── deps/ +├── lib/ +│ ├── hello/ +│ └── hello_web/ +│ ├── channels/ +│ ├── controllers/ +│ ├── templates/ +│ ├── views/ +│ ├── router.ex +│ └── gettext.ex +├── hello.ex +├── hello_web.ex +├── priv/ +└── test/ ``` +{: .-box-chars} -This is Phoenix 1.2's structure. Phoenix 1.3 has no `models`. +See: [Adding pages](https://hexdocs.pm/phoenix/adding_pages.html) _(hexdocs.pm)_ ### Migrations @@ -101,6 +117,5 @@ $ mix phx.gen.html \ ### Also see -- [Plug.Conn](./phoenix-conn.html) -- [Ecto migrations](./phoenix-migrations.html) -- [Router](./phoenix-routing.html) +- [Phoenix framework site](http://phoenixframework.org/) _(phoenixframework.org)_ +- [Phoenix: getting started](https://hexdocs.pm/phoenix/overview.html) _(hexdocs.pm)_ diff --git a/phoenix@1.2.md b/phoenix@1.2.md new file mode 100644 index 000000000..2008efe70 --- /dev/null +++ b/phoenix@1.2.md @@ -0,0 +1,23 @@ +--- +title: Phoenix 1.2 +category: Elixir +layout: 2017/sheet +weight: -1 +updated: 2017-09-04 +--- + +See [Phoenix](./phoenix) for a more updated cheatsheet. + +### Directory structure (Legacy 1.2) + +``` +config/ +web/ + controllers/ + models/ + views/ + templates/ + static/ +``` + +This is Phoenix 1.2's structure. Phoenix 1.3 has no `models`.