Route Helpers on forwarded routes and nested routers

148 views
Skip to first unread message

simonp...@gmail.com

unread,
Nov 16, 2015, 12:43:44 PM11/16/15
to phoenix-talk
Hi,

I’ve been using many routers to separate routing logics. This means that my router looks like this:

defmodule AccentApi.Router do
 
use AccentApi.Web, :router

  scope
"/", AccentApi do
    forward
"languages", Routers.Languages
 
end
end

And my Routers.Languages looks like:

defmodule AccentApi.Routers.Languages do
 
use AccentApi.Web, :router

  scope
"/", AccentApi do
   
get "/", LanguageController, :index
   
get "/:language_id", LanguageController, :show
 
end
end

Problem is, when using the AccentApi.Routers.Languages.Helper.language_path(:show, 1), the result is "/1". This is because the scope is "/" and the actual route "/languages/1" is catched by the forward function in the main Router.

Is their an easy way to use route helpers with forwarded routes? Or am I doing something wrong?

Thanks!

Stephen M

unread,
Nov 16, 2015, 6:24:10 PM11/16/15
to phoenix-talk, simonp...@gmail.com
Hi,

You could try something along the lines of "/languages" <> AccentApi.Routers.Languages.Helper.language_path(:show, 1)

What output do you get when you run `mix phoenix.routes` in the command line?

.
The plug conn struct has fields path_info and script_name have path information that might be of use.

Chris McCord

unread,
Nov 16, 2015, 6:35:50 PM11/16/15
to phoeni...@googlegroups.com
If you pass the `conn` to the route helper, it will maintain the prefix. Without the conn, there is no context to build the prefix with.

-- 
You received this message because you are subscribed to the Google Groups "phoenix-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to phoenix-talk...@googlegroups.com.
To post to this group, send email to phoeni...@googlegroups.com.
Visit this group at http://groups.google.com/group/phoenix-talk.
To view this discussion on the web visit https://groups.google.com/d/msgid/phoenix-talk/686c22c3-5650-45e5-802c-8719dad1dc0d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stephen M

unread,
Nov 16, 2015, 6:51:29 PM11/16/15
to phoenix-talk
I hadn't noticed that. Useful info. Thanks.
Reply all
Reply to author
Forward
0 new messages