Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add routing cheatsheet #5605

Merged
merged 5 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions guides/cheatsheets/router.cheatmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Routing cheatsheet

> Those need to be declared in the correct router module and scope.

A quick reference to the common routing features' syntax. For an exhaustive overview, refer to the [routing guides](routing.md).

## Routing declaration
{: .col-2}

### Single route

```elixir
get "/users", UserController, :index
patch "/users/:id", UserController, :update
```
```elixir
# generated path helpers
user_path(conn, :index) # /users
user_path(conn, :create, %{id: 9, ...}) # /users/9
```
Also accepts `put`, `patch`, `options`, `delete` and `head`.

### Resources

#### Simple

```elixir
resources "/users", UserController
```
Generates `:index`, `:edit`, `:new`, `:show`, `:create`, `:update` and `:delete`.

#### Options

```elixir
resources "/users", UserController, only: [:show]
resources "/users", UserController, except: [:create, :delete]
resources "/users", UserController, as: :person # `person_path(...)`
```

#### Nested

```elixir
resources "/users", UserController do
resources "/posts", PostController
end
```
```elixir
# generated path helpers
user_post_path(:index, 17) # /users/17/posts
user_post_path(:show, 17, 12) # /users/17/posts/12
```
For more info check the [resources docs.](routing-1.html#resources)

### Scopes

#### Simple
```elixir
scope "/admin", HelloWeb.Admin do
pipe_through :browser

resources "/users", UserController
end
```
```elixir
# generated path helpers
admin_user_path(:index) # /admin/users
```

#### Nested
```elixir
scope "/api", HelloWeb.Api, as: :api do
pipe_through :api

scope "/v1", V1, as: :v1 do
resources "/users", UserController
end
end
```
```elixir
# generated path helpers
api_v1_user_path(:index) # /api/v1/users
```
For more info check the [scoped routes](routing-1.html#scoped-routes) docs.
josevalim marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ defmodule Phoenix.MixProject do
"guides/howto/file_uploads.md",
"guides/howto/using_ssl.md",
"guides/howto/writing_a_channels_client.md",
"guides/cheatsheets/routing.cheatmd",
josevalim marked this conversation as resolved.
Show resolved Hide resolved
"CHANGELOG.md"
]
end
Expand All @@ -183,6 +184,7 @@ defmodule Phoenix.MixProject do
"Real-time": ~r/guides\/real_time\/.?/,
Testing: ~r/guides\/testing\/.?/,
Deployment: ~r/guides\/deployment\/.?/,
Cheatsheets: ~r/guides\/cheatsheets\/.?/,
"How-to's": ~r/guides\/howto\/.?/
]
end
Expand Down