2016年8月8日月曜日

「Phoenix」フレームワークでルーティング(Routing)その2

Phoenix」フレームワークでルーティング(Routing)その2です。resources で指定すると、CRUDのURLをすべて用意してくれます。更に、onlyexcept で細かく制御が可能。また、scope を利用すると、URLをまとめて管理することができます。

/web/router.ex
scope "/", HelloPhoenix do
  pipe_through :browser # Use the default browser stack

  get "/", PageController, :index
  resources "/users", UserController do
    resources "/posts", PostController, only: [:index, :show]
    resources "/comments", CommentController, except: [:delete]
  end
end

scope "/admin", HelloPhoenix.Admin, as: :admin do
  pipe_through :browser

  resources "/images",  ImageController
  resources "/reviews", ReviewController
  resources "/users",   UserController
end
コントローラーが存在しなくても、ルートの確認はできます。
$ mix phoenix.routes

        page_path  GET     /                                  HelloPhoenix.PageController :index
        user_path  GET     /users                             HelloPhoenix.UserController :index
        user_path  GET     /users/:id/edit                    HelloPhoenix.UserController :edit
        user_path  GET     /users/new                         HelloPhoenix.UserController :new
        user_path  GET     /users/:id                         HelloPhoenix.UserController :show
        user_path  POST    /users                             HelloPhoenix.UserController :create
        user_path  PATCH   /users/:id                         HelloPhoenix.UserController :update
                   PUT     /users/:id                         HelloPhoenix.UserController :update
        user_path  DELETE  /users/:id                         HelloPhoenix.UserController :delete
   user_post_path  GET     /users/:user_id/posts              HelloPhoenix.PostController :index
   user_post_path  GET     /users/:user_id/posts/:id          HelloPhoenix.PostController :show
user_comment_path  GET     /users/:user_id/comments           HelloPhoenix.CommentController :index
user_comment_path  GET     /users/:user_id/comments/:id/edit  HelloPhoenix.CommentController :edit
user_comment_path  GET     /users/:user_id/comments/new       HelloPhoenix.CommentController :new
user_comment_path  GET     /users/:user_id/comments/:id       HelloPhoenix.CommentController :show
user_comment_path  POST    /users/:user_id/comments           HelloPhoenix.CommentController :create
user_comment_path  PATCH   /users/:user_id/comments/:id       HelloPhoenix.CommentController :update
                   PUT     /users/:user_id/comments/:id       HelloPhoenix.CommentController :update
 admin_image_path  GET     /admin/images                      HelloPhoenix.Admin.ImageController :index
 admin_image_path  GET     /admin/images/:id/edit             HelloPhoenix.Admin.ImageController :edit
 admin_image_path  GET     /admin/images/new                  HelloPhoenix.Admin.ImageController :new
 admin_image_path  GET     /admin/images/:id                  HelloPhoenix.Admin.ImageController :show
 admin_image_path  POST    /admin/images                      HelloPhoenix.Admin.ImageController :create
 admin_image_path  PATCH   /admin/images/:id                  HelloPhoenix.Admin.ImageController :update
                   PUT     /admin/images/:id                  HelloPhoenix.Admin.ImageController :update
 admin_image_path  DELETE  /admin/images/:id                  HelloPhoenix.Admin.ImageController :delete
admin_review_path  GET     /admin/reviews                     HelloPhoenix.Admin.ReviewController :index
admin_review_path  GET     /admin/reviews/:id/edit            HelloPhoenix.Admin.ReviewController :edit
admin_review_path  GET     /admin/reviews/new                 HelloPhoenix.Admin.ReviewController :new
admin_review_path  GET     /admin/reviews/:id                 HelloPhoenix.Admin.ReviewController :show
admin_review_path  POST    /admin/reviews                     HelloPhoenix.Admin.ReviewController :create
admin_review_path  PATCH   /admin/reviews/:id                 HelloPhoenix.Admin.ReviewController :update
                   PUT     /admin/reviews/:id                 HelloPhoenix.Admin.ReviewController :update
admin_review_path  DELETE  /admin/reviews/:id                 HelloPhoenix.Admin.ReviewController :delete
  admin_user_path  GET     /admin/users                       HelloPhoenix.Admin.UserController :index
  admin_user_path  GET     /admin/users/:id/edit              HelloPhoenix.Admin.UserController :edit
  admin_user_path  GET     /admin/users/new                   HelloPhoenix.Admin.UserController :new
  admin_user_path  GET     /admin/users/:id                   HelloPhoenix.Admin.UserController :show
  admin_user_path  POST    /admin/users                       HelloPhoenix.Admin.UserController :create
  admin_user_path  PATCH   /admin/users/:id                   HelloPhoenix.Admin.UserController :update
                   PUT     /admin/users/:id                   HelloPhoenix.Admin.UserController :update
  admin_user_path  DELETE  /admin/users/:id                   HelloPhoenix.Admin.UserController :delete
参考サイト
Routing(Phoenix)

0 件のコメント:

コメントを投稿