Morzaram

Morzaram

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.

Showing Posts 1 to 3

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 OP

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.

— 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 92995 915
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
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
GES233
I’m posting this in response to Jose’s recent tweet (Cr. link) : People are sleeping on Elixir for a coding harness: Hot-code swappi...
New
_mfierro
Hello, I wrote Stop My Hand, a Scattergories-like web application using Phoenix/LiveView as my learning project for Elixir (after readin...
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

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews