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

AstonJ
Are there any Elixir or Erlang libraries that help with this? I’ve been thinking how streaming services like twitch have exploded recentl...
New
sergio
There’s a new TIOBE index report that came out that shows Elixir is still not in the top 50 used languages. It also goes on to call Elix...
New
rms.mrcs
A couple of days ago I was discussing with a friend about different approaches to write microservices. He said that if he was going to w...
New
tmbb
This is a post to discuss the new Phoenix LiveView functionality. From Chris’s talk, it appears that they generate all HTML on the serve...
342 18595 126
New
chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
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
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New

Other popular topics Top

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 130579 1222
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement