Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to define custom routes in Phoenix?

Tags:

Let's say I want to create a resources with adding a couple of custom actions to it, the analogue in rails is:

resources :tasks do   member do     get :implement   end end 

Which will return me not only 7 standard routes, but 1 new:

GET /tasks/:id/implement 

How can I do it in phoenix?

like image 321
asiniy Avatar asked May 17 '16 04:05

asiniy


2 Answers

I want to improve Dogbert's answer a little bit:

resources "/tasks", TaskController do   get "/implement", TaskController, :implement, as: :implement end 

The only addition is as: :implement in the end of the route.

Thus you will get route named task_implement_path instead of ugly task_task_path.

like image 140
denis.peplin Avatar answered Oct 02 '22 16:10

denis.peplin


You can add a get inside the do block of resources.

web/router.ex

resources "/tasks", TaskController do   get "/implement", TaskController, :implement end 

$ mix phoenix.routes

     task_path  GET     /tasks                     MyApp.TaskController :index      task_path  GET     /tasks/:id/edit            MyApp.TaskController :edit      task_path  GET     /tasks/new                 MyApp.TaskController :new      task_path  GET     /tasks/:id                 MyApp.TaskController :show      task_path  POST    /tasks                     MyApp.TaskController :create      task_path  PATCH   /tasks/:id                 MyApp.TaskController :update                 PUT     /tasks/:id                 MyApp.TaskController :update      task_path  DELETE  /tasks/:id                 MyApp.TaskController :delete task_task_path  GET     /tasks/:task_id/implement  MyApp.TaskController :implement 
like image 21
Dogbert Avatar answered Oct 02 '22 15:10

Dogbert