11
votes

How do i view the output of a route in iex?

I know i can do this with mix Phoenix.routes but would like to do this interactively.

Here is an example of what I would like to do:

iex -S Phoenix.server

mymodel_path

this gives me this error:

** (CompileError) iex:2: undefined function mymodel_path/0
6

6 Answers

11
votes
iex> Mix.Tasks.Phoenix.Routes.run ''                                              
page_path  GET   /           YourApp.PageController :index                                
user_path  GET   /users      YourApp.UserController :index                               
user_path  GET   /users/new  YourApp.UserController :new                                  
user_path  GET   /users/:id  YourApp.UserController :show                               
user_path  POST  /users      YourApp.UserController :create

Phoenix 1.3 update: mix phoenix.routes is deprecated. Use phx.routes instead. That is:

iex(7)> Mix.Tasks.Phx.Routes.run ''
 page_path  GET  /       HelloWeb.PageController :index
hello_path  GET  /hello  HelloWeb.HelloController :index
10
votes

All the url/path helpers are compiled into functions in the module YourApp.Router.Helpers. You can import it and call with the same arguments as you would in your templates (you would probably pass conn as the first argument but since we don't have a conn in the iex session, you can pass YourApp.Endpoint instead):

iex(1)> import YourApp.Router.Helpers
nil
iex(2)> page_path(YourApp.Endpoint, :index)
"/"
iex(3)> task_path(YourApp.Endpoint, :show, 1)
"/tasks/1"

(I have a resources "/tasks", TaskController in this project.)

2
votes
YourApp.Web.Router.__routes__()
1
votes

You can find the appropriate documentation for your requiement at the official docs here - http://www.phoenixframework.org/docs/routing#section-path-helpers

Assuming your app name is HelloPhoenix

iex> import HelloPhoenix.Router.Helpers
iex> alias HelloPhoenix.Endpoint
iex> user_path(Endpoint, :index)
"/users"

iex> user_path(Endpoint, :show, 17)
"/users/17"

iex> user_path(Endpoint, :new)
"/users/new"

iex> user_path(Endpoint, :create)
"/users"
0
votes

for umbrella app you should use

Mix.Tasks.Phx.Routes.run([YourApp.Web.Router])
0
votes

Assume: Project name = Mango I have created a Registration Controller (:new,:create) and a CategoryController(:show)

from iex:

alias MangoWeb.Router.Helpers , as: Routes 
alias MangoWeb.Endpoint 

Routes.page_path(Endpoint,:index)
"/"  

Routes.category_path(Endpoint,:show,17) 
"/categories/17"

Routes.registration_path(Endpoint,:new)
"/register" 

Routes.registration_path(Endpoint,:create)
"/register" 

Routes.category_path(Endpoint,:show,"fruit")
"/categories/fruit"

Routes.page_url(Endpoint,:index)
"http://localhost:4000/"

Routes.category_url(Endpoint,:show,"fruit")
"http://localhost:4000/categories/fruit"  

Routes.category_url(Endpoint,:show,"vegetables")
"http://localhost:4000/categories/vegetables"

Routes.registration_url(Endpoint,:new)
"http://localhost:4000/register"

Routes.registration_url(Endpoint,:create)
"http://localhost:4000/register" 
etc.