phoenix_localized_routes
phoenix_localized_routes copied to clipboard
Subdomain routing with scope host prefixes
Hello,
I was wondering if you think it would be possible to adapt this library to support subdomain routing?
For example, the Phoenix "scope" macro has a "host" key that will accept a prefix like this:
scope "/", ExampleWeb do
pipe_through :browser
get "/xyz", PageController, :index, as: "en_xyz"
end
scope "/", ExampleWeb, host: "de." do
pipe_through :browser
get "/xyz", PageController, :index, as: "de_xyz"
end
Which produces the following phx.routes:
en_xyz_path GET /xyz ExampleWeb.PageController :index
de_xyz_path GET /xyz ExampleWeb.PageController :index
In this case the paths are equal and depending on which subdomain is provided, the different scope is executed. This would allow for cases where the same word exists in multiple languages. But these paths could also be different per language like they currently exist.
I was thinking to combine this with the "assigns" key on the "scope" macro to pass the locale into the connection automatically.
I'm happy to try and tackle this and contribute it via a PR but I was wondering if you were interested in this and/or whether you have advice on how to approach it.
Thanks
Hi Ben,
Nice to see interest in the lib and your offer to help. I would very much like such functionality!
Without having looked at the subdomain func. in Phoenix I guess when no subdomain is given, it acts as a fallback. So the change to the configuration could be small.
scopes: %{
"/" => %{
assign: %Assigns{contact: "[email protected]"},
scopes: %{
"europe." => %{
assign: %Assigns{contact: "[email protected]"},
scopes: %{
"/nl" => %{assigns: %Assigns{locale: "nl", contact: "[email protected]"}},
"/be" => %{assigns: %Assigns{locale: "nl", contact: "[email protected]"}}
}
},
"asia." => %{assign: %Assigns{contact: "[email protected]"}
}
},
I was thinking to combine this with the "assigns" key on the "scope" macro to pass the locale into the connection automatically.
The "assigns" key should remain free form in the core Config module. I still have to decide if I want to write some convenience adapters (preprocessing) or go with a 'plug' edition (in Config pipeline processing) that will do what you suggest. However, it's a separate feature and should have it's own ticket.
Would love to see a PR. Feel free to ask me any questions you might have. If you send me a PM at Elixir Forum I will send you my contact details.