最終的な目標→Phoenixで何か作る。
手始めにルーティングをいじってみる。
とりあえずプロジェクト作成 。mix phoenix.new sparrow
プロジェクト名は単なる思いつき。
で、ルーティングはweb/router.ex
をいじる。標準ではこんな感じ。
web/router.ex
defmodule Sparrow.Router do
use Sparrow.Web, :router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
end
pipeline :api do
plug :accepts, ["json"]
end
scope "/", Sparrow do
pipe_through :browser # Use the default browser stack
get "/", PageController, :index
end
# Other scopes may use custom stacks.
# scope "/api", Sparrow do
# pipe_through :api
# end
end
書き方はRailsのルーティングと似たような感じらしい。
とりあえず
web/router.ex
defmodule Sparrow.Router do
use Sparrow.Web, :router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
end
pipeline :api do
plug :accepts, ["json"]
end
scope "/", Sparrow do
pipe_through :browser # Use the default browser stack
get "/", PageController, :index
get "/crow", CrowController, :index
get "/crow/:id", CrowController, :show
post "/crow", CrowController, :create
end
# Other scopes may use custom stacks.
# scope "/api", Sparrow do
# pipe_through :api
# end
end
で、mix phoenix.routes
page_path GET / Sparrow.PageController :index
crow_path GET /crow Sparrow.CrowController :index
crow_path GET /crow/:id Sparrow.CrowController :show
crow_path POST /crow Sparrow.CrowController :create
こんな感じ。コントローラを書いておく必要は特にない。
REST形式のAPIを作成するのにはresourcesが使える。
resource(Railsでは単一のリソース)は使えないみたい。
web/router.ex
defmodule Sparrow.Router do
use Sparrow.Web, :router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
end
pipeline :api do
plug :accepts, ["json"]
end
scope "/", Sparrow do
pipe_through :browser # Use the default browser stack
get "/", PageController, :index
# resource "/eagle", EagleController <- error
resources "/eagle", EagleController
end
# Other scopes may use custom stacks.
# scope "/api", Sparrow do
# pipe_through :api
# end
end
page_path GET / Sparrow.PageController :index
eagle_path GET /eagle Sparrow.EagleController :index
eagle_path GET /eagle/:id/edit Sparrow.EagleController :edit
eagle_path GET /eagle/new Sparrow.EagleController :new
eagle_path GET /eagle/:id Sparrow.EagleController :show
eagle_path POST /eagle Sparrow.EagleController :create
eagle_path PATCH /eagle/:id Sparrow.EagleController :update
PUT /eagle/:id Sparrow.EagleController :update
eagle_path DELETE /eagle/:id Sparrow.EagleController :delete
exceptやonlyは使用可能。
また、scopeは入れ子に出来、asの指定も可能。
web/router.ex
defmodule Sparrow.Router do
use Sparrow.Web, :router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
end
pipeline :api do
plug :accepts, ["json"]
end
scope "/", Sparrow do
pipe_through :browser # Use the default browser stack
get "/", PageController, :index
scope "/admin", Admin, as: :admin do
resources "/eagle", EagleController, only: [:index, :show]
resources "/hawk", HawkController, except: [:delete]
end
end
# Other scopes may use custom stacks.
# scope "/api", Sparrow do
# pipe_through :api
# end
end
page_path GET / Sparrow.PageController :index
admin_eagle_path GET /admin/eagle Sparrow.Admin.EagleController :index
admin_eagle_path GET /admin/eagle/:id Sparrow.Admin.EagleController :show
admin_hawk_path GET /admin/hawk Sparrow.Admin.HawkController :index
admin_hawk_path GET /admin/hawk/:id/edit Sparrow.Admin.HawkController :edit
admin_hawk_path GET /admin/hawk/new Sparrow.Admin.HawkController :new
admin_hawk_path GET /admin/hawk/:id Sparrow.Admin.HawkController :show
admin_hawk_path POST /admin/hawk Sparrow.Admin.HawkController :create
admin_hawk_path PATCH /admin/hawk/:id Sparrow.Admin.HawkController :update
PUT /admin/hawk/:id Sparrow.Admin.HawkController :update
とりあえずこんな感じ。