Morzaram

Morzaram

Thoughts on File Based Routing?

Hey guys, I was wondering what everyone’s take on file-based routing? I know Phoenix has very much the style of Rails Routes setup but the flow with file-based routing feels really smooth for me. I was wondering if y’all feel the same way?

Prime examples of this can be seen in many of the front-end frameworks :
Next
Remix
SvelteKit

I was contemplating making a library that leverages what Phoenix offers but through file-based routing. Would others be interested in this? Is this even possible?

I’ve got a lot to learn to implement this, but I really enjoy Phoenix & Elixir and I think the philosophy of being a wrapper around your app vs the backbone of your app can be leveraged further in this way.

Would love y’alls thoughts on this, and sorry if this is really dumb.

Most Liked

LostKobrakai

LostKobrakai

I’d imagine you could build a router at compile time based on files quite well. Though I’d be curious if people actually have a single file per route, because for my projects that’s hardly the case.

Morzaram

Morzaram

I was thinking things can be very much like how SvelteKit shadow points are, and how we have a live structure now actually. Just spit balling here:

├── post
│   ├── new.ex
│   ├── new.html.heex #Optional if you have a render call in index.ex (seen below)
│   ├── [id].ex

Within this, we can have predefined functions of [get, post, put, del] very much how we have it in the live controller with [mount, handle_event, handle_info, update, etc.]

ie: new.ex would have

  def get(conn, _params) do
    # Logic
    render(conn, "index.html")
  end

  def post(conn, _params) do
    # Logic
    render(conn, "index.html")
  end

# This can be separated into separate heex page if wanted, as we currently have in phx
  def render(assigns) do 
    ~H"""
     <form></form>
     """
  end

And then when we run mix phx.routes we can see naming of

post_new_path GET /posts ??? :new
post_path GET /posts/id ??? :id

Now I’m not sure how this would work live_view wise but REST wise it would work right?

al2o3cr

al2o3cr

Not quite, typically REST doesn’t POST to /resources/new. It would look something like:

  • resource
    • new.ex - has a get function (new in standard Phoenix)
    • index.ex - has a get (Phoenix index) and a post (Phoenix create) function
    • [id].ex - has a get (Phoenix show), a put (Phoenix update) and a delete (Phoenix delete)
    • [id]/edit.ex has a get (Phoenix edit)

One big challenge I see with this approach is that it’s not possible to tell which HTTP verbs a particular file supports without compiling it. That would make compiling route helpers a circular dependency: the files that talk HTTP define the routes but also USE them.

Some things that are split apart in this approach (new vs create for instance) are IMO better off together, because they depend on common templates and therefore need to set the same assigns.

Where Next?

Popular in Discussions Top

axelson
Decided against including more info in the title, but the gist is that Plataformatec sponsored projects will continue with the assets bei...
New
ricklove
I was just introduced to Elixir and Phoenix. I was told about the 2 million websocket test that was done 2 years ago. From my research, t...
New
crispinb
On reading dhh’s latest The One Person Framework it strikes me that Phoenix with LiveView is already pretty much this. However, never hav...
New
crabonature
I’m still quite new to Elixir. As I understand we got in Elixir “multi guards” as convention to simplify one large guard with or’s?: de...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
PragTob
Hey everyone, this has been on my mind for some time and I’d love your input on it! TLDR: I feel like maps are superioer for storing and...
New
RudManusachi
What configs will make sense to put to runtime.exs? – A bit of how I configure apps: I have generic configs in config/config.exs, dev...
New
chulkilee
Here are the list of HTTP client libraries/wrappers, and some thoughts on HTTP client in general. I’d like to hear from others how they w...
New
Owens
Hello all, I am developing a new mobile app with Flutter frontend and Phoenix backend. The mobile app has real-time task management and c...
New
paulanthonywilson
I like Umbrella projects and pretty much always use them for personal Elixir stuff, especially Nerves things. But I don’t think this is ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New

We're in Beta

About us Mission Statement