thojanssens1

thojanssens1

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?

Showing Posts 5 to 1

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

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

wolfiton

wolfiton

One command that i find very useful when dealing with routes is mix phx.routes it shows you what routes you have.

Also have a look at this Controllers — Phoenix v1.8.8 it should show you how to redirect with different options.

Hope that it helps

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.

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.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
maennchen
:warning: Security advisory: Decimal DoS vulnerability A vulnerability has been published for decimal where very large exponents can cau...
New
marciol
It would be helpful to have a list of companies worldwide that hire engineers without prior experience in Elixir. Often, it can be quite ...
New
durvia
Anyone running long-lived stateful processes on BEAM? We’re building an AI agent runtime and would love to compare notes. We’re a small ...
New

Other Trending Topics Top

marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews