thojanssens1

thojanssens1

Phoenix - redirects in router file

It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.:

redirect "/", UserController, :list
get "/users", UserController, :list

This is typically needed when you have a URL that you want the user to be able to type but that should be redirected to a more specific URL.

Ofc I can code an action and a redirect myself, but such a helper would avoid me to add that code that could be explicitly defined in the router; you can then quickly see that there is a redirect along with the other routes, without inspecting the controller’s code. I think such redirects without any logic belongs there.

What do you think about that idea?

Marked As Solved

mathieuprog

mathieuprog

The redirect library addresses this shortcoming by providing with a redirect/3 macro, allowing to redirect a request from one path to another in the router.exfile:

redirect "/", "/users", :permanent # 301 redirect
redirect "/", "/users", :temporary # 302 redirect

https://github.com/mathieuprog/redirect

Also Liked

jakeprem

jakeprem

We ran into this on the project I’m working on. We found this blog post to be a pretty good way to do this: How to Redirect from the Phoenix Router | Viget

We ended up with a plug that lets us do this:

get "/", Redirect, to: "/datasets"

You can see our implementation here: smartcitiesdata/apps/andi/lib/andi_web/plugs/redirect.ex at master · UrbanOS-Public/smartcitiesdata · GitHub
smartcitiesdata/apps/andi/lib/andi_web/router.ex at master · UrbanOS-Public/smartcitiesdata · GitHub

It looks like there is a plug_redirect package in Hex that does adds a redirect keyword. Their Readme suggests using it as its own Plug in your endpoint, but at a quick glance I don’t see any reason you couldn’t use it in a Phoenix router

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I think this is a good idea, if a little niche. although you’d want to implement it as redirect "/", "/users" because redirect "/", UserController, :list is ambiguous. Multiple paths can route to the same controller action.

LostKobrakai

LostKobrakai

Phoenix.Controller.redirect/2 is a plug, and the generated router for phoenix has it imported by default. Together with forward you should be able to use it already without the need for additional code.

Where Next?

Popular in Discussions Top

blackode
Elixir Upgrading is so Simple in Ubuntu and It worked for me Ubuntu 16.04 git clone https://github.com/elixir-lang/elixir.git cd elixir...
New
Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New
Fl4m3Ph03n1x
Background A few days ago I was listening to The future of Elixir from Elixir Talks, with Dave Thomas (@pragdave ) and Brian Mitchell. I...
New
axelson
Decided against including more info in the title, but the gist is that Plataformatec sponsored projects will continue with the assets bei...
New
arcanemachine
https://nitter.net/josevalim/status/1744395345872683471 https://twitter.com/josevalim/status/1744395345872683471
New
AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 19568 166
New
AstonJ
If a newbie asked you about Phoenix Contexts, how would you explain the basics to them? Feel free to be as concise or in-depth as you li...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
marciol
Please, let me know if this kind of discussion already took place in another topic . Hi all, how do you consider if is better to build ...
New
rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

We're in Beta

About us Mission Statement