phoenix-routing: update

This commit is contained in:
Rico Sta. Cruz 2017-09-04 11:20:35 +08:00
parent 3a75d02f40
commit 0de186c8f1
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
3 changed files with 88 additions and 26 deletions

View File

@ -1,13 +1,20 @@
--- ---
title: "Phoenix: Routing" title: "Phoenix: Routing"
category: Elixir category: Elixir
layout: 2017/sheet
weight: -1
--- ---
### Showing routes
```sh ```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 ```elixir
get "/", PageController, :index get "/", PageController, :index
@ -15,19 +22,23 @@ get "/", PageController, :index
Also: `put` `post` `patch` `options` `delete` `head` Also: `put` `post` `patch` `options` `delete` `head`
## Resources ### Resources
```elixir ```elixir
resources "/users", UserController resources "/users", UserController
resources "/users", UserController, only: [:index, :show] resources "/users", UserController, only: [:index, :show]
resources "/users", UserController, except: [:delete] resources "/users", UserController, except: [:delete]
```
```elixir
resources "/users", UserController,
as: :person # helper name (person_path) as: :person # helper name (person_path)
name: :person # ...? name: :person # ...?
param: :id # name of parameter for this resource param: :id # name of parameter for this resource
``` ```
Generates these routes:
| Method | Path | Helper | | Method | Path | Helper |
| ---- | ---- | ---- | | ---- | ---- | ---- |
| GET | `/users` | `user_path(:index)` | | GET | `/users` | `user_path(:index)` |
@ -37,34 +48,45 @@ resources "/users", UserController, except: [:delete]
| POST | `/users` | `user_path(:create, user)` | | POST | `/users` | `user_path(:create, user)` |
| PATCH/PUT | `/users/:id` | `user_path(:update, user)` | | PATCH/PUT | `/users/:id` | `user_path(:update, user)` |
| DELETE | `/users/:id` | `user_path(:delete, 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 ```elixir
user_path(Endpoint, :index) #=> /users user_path(conn, :index) # → /users
user_path(Endpoint, :show, 17) #=> /users/17 user_path(conn, :show, 17) # → /users/17
user_path(Endpoint, :show, %User{id: 17}) #=> /users/17 user_path(conn, :show, %User{id: 17}) # → /users/17
user_path(Endpoint, :show, 17, admin: true) #=> /users/17?admin=true 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 ```elixir
MyApp.Router.Helpers.user_path(MyApp.Endpoint, :index) 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 ```elixir
resources "/users", UserController do resources "/users", UserController do
resources "/posts", PostController resources "/posts", PostController
end 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 ```elixir
scope "/admin" do scope "/admin" do
@ -78,3 +100,5 @@ end
scope "/admin", as: :admin do: ... end scope "/admin", as: :admin do: ... end
# admin_reviews_path() -> /admin/reviews # admin_reviews_path() -> /admin/reviews
``` ```
See: [scope/2](https://hexdocs.pm/phoenix/Phoenix.Router.html#scope/2) _(hexdocs.pm)_

View File

@ -9,16 +9,32 @@ updated: 2017-09-04
### Directory structure ### Directory structure
``` ```
config/ ./
web/ ├── _build
controllers/ ├── assets/
models/ │ ├── css/
views/ │ ├── js/
templates/ │ ├── static/
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 ### Migrations
@ -101,6 +117,5 @@ $ mix phx.gen.html \
### Also see ### Also see
- [Plug.Conn](./phoenix-conn.html) - [Phoenix framework site](http://phoenixframework.org/) _(phoenixframework.org)_
- [Ecto migrations](./phoenix-migrations.html) - [Phoenix: getting started](https://hexdocs.pm/phoenix/overview.html) _(hexdocs.pm)_
- [Router](./phoenix-routing.html)

23
phoenix@1.2.md Normal file
View File

@ -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`.