Skip to content

Commit

Permalink
Guides updates for Phoenix 1.4 (phoenixframework#3164)
Browse files Browse the repository at this point in the history
* Update the Up and Running and Routing guides for Phoenix 1.4

* Remove old installer routing template comment from guides
  • Loading branch information
michallepicki authored and Gazler committed Nov 16, 2018
1 parent b6540d8 commit 560fde8
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 26 deletions.
6 changes: 3 additions & 3 deletions guides/adding_pages.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ defmodule HelloWeb.Router do
end

scope "/", HelloWeb do
pipe_through :browser # Use the default browser stack
pipe_through :browser

get "/", PageController, :index
end
Expand All @@ -134,7 +134,7 @@ The `scope "/"` block of our `router.ex` file should now look like this:

```elixir
scope "/", HelloWeb do
pipe_through :browser # Use the default browser stack
pipe_through :browser

get "/", PageController, :index
get "/hello", HelloController, :index
Expand Down Expand Up @@ -218,7 +218,7 @@ For this exercise, we're going to re-use the `HelloController` we just created a

```elixir
scope "/", HelloWeb do
pipe_through :browser # Use the default browser stack.
pipe_through :browser

get "/", PageController, :index
get "/hello", HelloController, :index
Expand Down
4 changes: 2 additions & 2 deletions guides/contexts.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Phoenix generated the web files as expected in `lib/hello_web/`. We can also see

```elixir
scope "/", HelloWeb do
pipe_through :browser # Use the default browser stack
pipe_through :browser

get "/", PageController, :index
+ resources "/users", UserController
Expand Down Expand Up @@ -497,7 +497,7 @@ Next, let's wire up our session routes in `lib/hello_web/router.ex`:

```elixir
scope "/", HelloWeb do
pipe_through :browser # Use the default browser stack
pipe_through :browser

get "/", PageController, :index
resources "/users", UserController
Expand Down
21 changes: 9 additions & 12 deletions guides/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule HelloWeb.Router do
end

scope "/", HelloWeb do
pipe_through :browser # Use the default browser stack
pipe_through :browser

get "/", PageController, :index
end
Expand Down Expand Up @@ -92,7 +92,7 @@ Let's add a resource to our `lib/hello_web/router.ex` file like this:

```elixir
scope "/", HelloWeb do
pipe_through :browser # Use the default browser stack
pipe_through :browser

get "/", PageController, :index
resources "/users", UserController
Expand Down Expand Up @@ -797,7 +797,6 @@ defmodule HelloWeb.Router do
...

scope "/reviews" do
# Use the default browser stack.
pipe_through [:browser, :review_checks, :other_great_stuff]

resources "/", HelloWeb.ReviewController
Expand Down Expand Up @@ -873,13 +872,17 @@ We mount socket handlers in our endpoint at `lib/hello_web/endpoint.ex`. Socket

```elixir
defmodule HelloWeb.Endpoint do
use Phoenix.Endpoint
use Phoenix.Endpoint, otp_app: :hello

socket "/socket", HelloWeb.UserSocket
socket "/socket", HelloWeb.UserSocket,
websocket: true,
longpoll: false
...
end
```

By default, Phoenix supports both websockets and longpoll when invoking Phoenix.Endpoint.socket/3 in your endpoint. Here we're specifying that incoming socket connections can be made via a WebSocket connection.

Next, we need to open our `lib/hello_web/channels/user_socket.ex` file and use the `channel/3` macro to define our channel routes. The routes will match a topic pattern to a channel to handle events. If we have a channel module called `RoomChannel` and a topic called `"rooms:*"`, the code to do this is straightforward.

```elixir
Expand All @@ -893,16 +896,10 @@ end

Topics are just string identifiers. The form we are using here is a convention which allows us to define topics and subtopics in the same string - "topic:subtopic". The `*` is a wildcard character which allows us to match on any subtopic, so `"rooms:lobby"` and `"rooms:kitchen"` would both match this route.

Phoenix abstracts the socket transport layer and includes two transport mechanisms out of the box - WebSockets and Long-Polling. If we wanted to make sure that our channel is handled by only one type of transport, we could specify that using the `via` option, like this.

```elixir
channel "rooms:*", HelloWeb.RoomChannel, via: [Phoenix.Transports.WebSocket]
```

Each socket can handle requests for multiple channels.

```elixir
channel "rooms:*", HelloWeb.RoomChannel, via: [Phoenix.Transports.WebSocket]
channel "rooms:*", HelloWeb.RoomChannel
channel "foods:*", HelloWeb.FoodChannel
```

Expand Down
2 changes: 1 addition & 1 deletion guides/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule HelloWeb.Router do
...

scope "/", HelloWeb do
pipe_through :browser # Use the default browser stack
pipe_through :browser

get "/", PageController, :index
get "/test", PageController, :test
Expand Down
2 changes: 1 addition & 1 deletion guides/testing/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ defmodule HelloWeb.Router do
...

scope "/", Hello do
pipe_through :browser # Use the default browser stack
pipe_through :browser

get "/", PageController, :index
resources "/users", UserController
Expand Down
2 changes: 1 addition & 1 deletion guides/testing/testing_controllers.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ defmodule HelloWeb.Router do
end

scope "/", HelloWeb do
pipe_through :browser # Use the default browser stack
pipe_through :browser

get "/", PageController, :index
end
Expand Down
2 changes: 1 addition & 1 deletion guides/testing/testing_schemas.md
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ defmodule HelloWeb.Router do
...

scope "/", HelloWeb do
pipe_through :browser # Use the default browser stack
pipe_through :browser

get "/", PageController, :index
resources "/users", UserController
Expand Down
14 changes: 9 additions & 5 deletions guides/up_and_running.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ mix phx.new hello
* creating hello/config/dev.exs
* creating hello/config/prod.exs
...
* creating hello/lib/hello_web/views/layout_view.ex
* creating hello/lib/hello_web/views/page_view.ex
* creating hello/assets/static/images/phoenix.png
* creating hello/assets/static/favicon.ico

Fetch and install dependencies? [Yn]
```
Expand All @@ -40,7 +40,7 @@ Fetch and install dependencies? [Yn] Y
* running mix deps.compile
* running cd assets && npm install && node node_modules/webpack/bin/webpack.js --mode development

We are all set! Go into your application by running:
We are almost there! The following steps are missing:

$ cd hello

Expand Down Expand Up @@ -69,6 +69,8 @@ Now we'll create our database:

```
$ mix ecto.create
Compiling 13 files (.ex)
Generated hello app
The database for Hello.Repo has been created
```

Expand All @@ -78,8 +80,10 @@ And finally, we'll start the Phoenix server:

```console
$ mix phx.server
[info] Running HelloWeb.Endpoint with Cowboy using http://0.0.0.0:4000
19:30:43 - info: compiled 6 files into 2 files, copied 3 in 2.1 sec
[info] Running HelloWeb.Endpoint with cowboy 2.5.0 at http://localhost:4000

Webpack is watching the files…
...
```

If we choose not to have Phoenix install our dependencies when we generate a new application, the `phx.new` task will prompt us to take the necessary steps when we do want to install them.
Expand Down

0 comments on commit 560fde8

Please sign in to comment.