LoginSignup
9
3

More than 5 years have passed since last update.

phoenixを触ってみる その2

Posted at

前回 の続き
https://hexdocs.pm/phoenix/adding_pages.html#content
に沿ってページの追加やっていきます。

静的ページの追加

パスからコントローラへのルーティングを追加します。

hello/lib/hello_web/router.ex
  scope "/", HelloWeb do
    pipe_through :browser # Use the default browser stack

    get "/", PageController, :index
    get "/hello", HelloController, :index # ★追加
  end

んでコントローラも作ります。

hello/lib/hello_web/controllers/hello_controller.ex
defmodule HelloWeb.HelloController do
  use HelloWeb, :controller

  def index(conn, _params) do
    render conn, "index.html"
  end
end

connにはリクエストの情報やらレスポンスに使う情報やらが格納されています。
https://hexdocs.pm/plug/Plug.Conn.html

paramは今回使わないので、"_"付き

続いてビュー側。
Viewとtemplateを作成します。

hello/lib/hello_web/views/hello_view.ex
defmodule HelloWeb.HelloView do
  use HelloWeb, :view
end
hello/lib/hello_web/templates/hello/index.html.eex
<div class="jumbotron">
  <h2>Hello World, from Phoenix!</h2>
</div>

表示用にデータの加工したりするのがView、
実際に表示するhtmlを組み立てるのがtemplateです。

controllerからはindex.htmlとしか指定してないですが、パスは名前で解決される模様。

スクリーンショット 2018-07-28 17.28.16.png

templateの割にいろいろ表示されてますね。
hello/lib/hello_web/templates/layout/app.html.eex を見ると <%= render @view_module, @view_template, assigns %> といった記述があるので、ここに埋め込まれるみたいですね。
renderはまずlayoutViewから行われるようです。設定とかで変えられるのかな。
この辺の仕組みは後で掘ってみるとして。

動的ページ

次はパラメータを受け取り、それを表示させます。
Controllerは先程のHelloControllerをそのまま使います。

hello/lib/hello_web/router.ex
  scope "/", HelloWeb do
    pipe_through :browser # Use the default browser stack

    get "/", PageController, :index
    get "/hello", HelloController, :index
    get "/hello/:messenger", HelloController, :show # ★追加
  end
hello/lib/hello_web/controllers/hello_controller.ex
  def show(conn, %{"messenger" => messenger}) do
    render conn, "show.html", messenger: messenger
  end
hello/lib/hello_web/templates/hello/show.html.eex
<div class="jumbotron">
  <h2>Hello World, from <%= @messenger %> !</h2>
</div>

スクリーンショット 2018-07-28 17.54.07.png

routerで指定した:messengerがマップに入ってコントローラに渡るようです。
これは、つまり、パターンマッチが出来るのか・・・!

いったんAddingPagesは終わりですが、実験。

hello/lib/hello_web/controllers/hello_controller.ex
def show(conn, %{"messenger" => messenger}) when messenger=="utonton" do
  render conn, "special.html", messenger: messenger
end

def show(conn, %{"messenger" => messenger}) do
  render conn, "show.html", messenger: messenger
end
hello/lib/hello_web/templates/hello/special.html.eex
<div class="jumbotron">
  <h2>Hey! You are special message from <%= @messenger %> !</h2>
</div>

スクリーンショット 2018-07-28 17.54.43.png

・・・できちゃったよ。
これ、意外と便利なんじゃなかろうか。コントローラ内で分岐するよりだいぶスッキリしてる。
しかもコントローラ内で変な分岐入れるのって結構抵抗あるけど、
こういった形なら書きやすいし、何より意図が伝わりやすそう。

9
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
3