Gladear

Gladear

Proposal for Phoenix: add HTTP verb to VerifiedRoutes

Hello everyone!

tldr; I propose to add a way for VerifiedRoutes to know if they’re used for get, post, etc to make them even more robust, and safer to refactor. This solution could be to add a “tag” to verified routes, like so: ~p"/user/#{user}"get.

Context

So, I’ve been using Phoenix for a while, both personally and professionally, and was reaaaaally glad when verified routes were introduced. Compared to the router helpers, they are shorter and easier to read, safer, and help ease your mind when introducing new routes.
There is however one shortcoming I have come across multiple times already. Let’s create some context:
I’ve got two routes with different verbs, but with the same name

# router.ex

get "/users/:id", UserController, :show
put "/users/:id", UserController, :update

Let’s say I want to rename the put route to end with /edit to match a new :edit route

get "/users/:id", UserController, :show
get "/users/:id/edit", UserController, :edit
put "/users/:id/edit", UserController, :update

This change is never going to be caught by verified routes, whilst I think it could be really handy, at the very least for large applications (and for control freaks, like the one I am).

Proposal

Although I do have a proposal, my main intention is to solve this issue I’ve been having quite some times already, so feel free to propose other options :blush:

The simplest way I found to fix this issue would be to add a “tag” to verified routes. It uses an existing feature of sigils, and from what I saw shouldn’t be too hard to implement.

~p"/users/#{user}"get # Still works after refactor
~p"/users/#{user}"put # Emits a warning after refactor, since `put "/user/:id"` does not exist anymore

It also has the advantage of being easily made optional

~p"/users/#{user}" # matches anything, making existing verified routes exactly the same as before

The most obvious drawbacks I can see for this proposal are

  • the process is manual, making it pretty error-prone (it’s easy to copy-paste a route and forget to update the tag)
  • I personally think it decreases readability of verified routes, but I think that it’s still an acceptable trade-off.

As a side note, I though about having an option to enforce usage, but I think that this should be the role of a linter like Credo, more than the library itself.

Sooo, here’s for the proposal. It’s my very first one so, sorry if I missed any obvious information :smile: I’m willing to try to implement this if this is something that you think is interesting.
Thanks for your time, and have a nice day :wave:

Most Liked

zachdaniel

zachdaniel

Creator of Ash

This is a good idea. However, I’d recommend this syntax:

~p"PUT /users/#{user}"
LostKobrakai

LostKobrakai

One drawback to this is that the url might not be directly correlated to an actual request being made. Yes you validate that a put request endpoint exists, but the code might do a patch instead - even just for an otherwise totally valid refactor. I would still consider this a useful addition especially given it‘s opt in.

Gladear

Gladear

@zachdaniel I like the idea, thanks for the input ! I saw ~p must always start with a / right now, so it should be fairly easy to differentiate both usages.

@sodapopcan There’s one big limitation with this syntax (~put"..."). HTTP verb can be anything, Phoenix supports matching custom ones - you can do match :custom, "/posts/custom", PostController, [] and the route will only match if the verb is CUSTOM. We can’t generate sigils for that (I mean, we could likely find a solution, but I don’t think it’s worth looking into it)

Where Next?

Popular in Proposals: Ideas Top

AHBruns
Rethinking phx-no-feedback So, I won’t go into how phx-no-feedback works in detail, but the tl;dr is that it lets you conditionally add a...
New
woylie
We are seeing a lot of warning logs like this: navigate event to "https://someurl" failed because you are redirecting across live_sessio...
New
sezaru
When writing my code, I always find __MODULE__ very useful to use as alias of that module “inner dependencies”, ex: alias __MODULE__.{Im...
New
Jskalc
Hi everyone! Recently I was thinking a lot about the way HEEX renders lists. People are generally surprised about huge payloads being sen...
New
pinetops
LiveView is by far my favorite web tech, but a few things have been nagging me. So with all the fancy and ill advised elixir tricks I cou...
New
sevensidedmarble
Hello all, Apologies if this has been proposed before I guess, but I have a very simple one: With the increasing importance of LV, I th...
New
caslu
Recently (last week) this PR was merged in Ruby on Rails repository and i thought it could be nice to have in phoenix, basically what it ...
New
lemaster
Currently we have stream_insert and stream that can either add new items to a stream or update existing items if they are already present...
New
eagle-head
Hi everyone, I’ve been researching Content Security Policy Level 3 support in Phoenix and wanted to share my findings and a proposal for...
New
MatinDevs
Currently, there are ongoing discussions about enhancing Phoenix LiveView, particularly focusing on improving performance and user experi...
New

Other popular topics Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement