Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Darkdawg's avatar

Fetch actual routes for dynamic routes?

Say you have a dynamic route, like: Route::get('/posts/{id}', [PostController::class, 'handle'])->name('posts.id');

As the source for these routes is unknown to the Route facade (could be database, could be blade files), it has no idea about the actual routes that are available under this dynamic route definition, I guess.

If the routes are defined in a database, then it's easy enough to just fetch them and list them, but is there an easy way to define and return file based routes?

For example, can you put name('posts/1') or something in the Blade file, so it will get registered (and then accessible by the Route facade)? Or do you simply have to retrieve all files under the specific folder and parse that? And is there some Laravel method to fetch all routes based on a specific name, or would I have to create something from scratch?

I'm curious as I would prefer having dynamic URL lists, for example if I make a widget with "All posts" or something.

Thanks!

0 likes
10 replies
tykus's avatar

As the source for these routes is unknown to the Route facade (could be database, could be blade files),

...file based routes...

Explain how the source for the Route might be a Blade file, or a file based route?

can you put name('posts/1') or something in the Blade file, so it will get registered

No.

It seems like you are thinking about the applications routes incorrectly; maybe explain the problem you are trying to solve?

Darkdawg's avatar

@tykus Sorry, my English/terminology is probably off!

With source I meant the pages that exist for the dynamic route. For a database, for example a url column with 10 entries, and the dynamic route points to those 10 routes. Or for file based approach, where the controller checks if a view exists for the given variables.

So for example, a dynamic route where the closure/controller checks if a view for the request exists:

Route::get('/articles/{category}', function ($category)
{
    $viewPath = "articles.{$category}";

    if (!view()->exists($viewPath)) {
        abort(404);
    }

    return view($viewPath, compact('category'));
});

I don't know if this is a standard approach, but I want a single source of truth for large sections of pages. For example, imagine we have 10 categories and 20 subcategories each. Defining a single route per page would be messy, and you would have to make sure the routes and views are always in sync.

EDIT: For reference, here's what I mean with a database based route approach:

public function handle($url)
{
    $post = Posts::where('url', $url)->first();

    if (!$post) {
        abort(404, 'Post not found');
    }
    return view('posts', [
        'content' => $post->content,
        ...
    ]);
}
Darkdawg's avatar

@Sinnbeck Right, I saw Folio and at first it seemed like just the thing I wanted...

But while it does create a route per file, it also seems to change the whole process of controllers and such? It also doesn't seem have an easy way to retrieve pages for a given section, or? I would probably be better off creating some custom methods that just fetch the files in that case 🤔

And can you mix the standard route definition with this? Some of my routes would come from a database.

Sinnbeck's avatar

@Darkdawg Not sure what you mean by retrieving pages for a given section? If you are using blade you just load one page. If you want to load multiple to stitch the page together, you need to load it using javascript, and then insert it into the page using js as well.

I have never used folio myself, as I dont like file based routing. I prefer regular routes with controllers, or livewire components. But I am sure you can use both if you want

Darkdawg's avatar

@Sinnbeck I mean, say you have a component that lists all cars under the cars category. With a file based approach you could have like views/cars/bmw.blade.php, views/cars/tesla.blade.php, and then you want an easy way to retreieve all cars under the cars category.

"All pages under cars: bmw telsa "

Now I wouldn't want to hardcode this, that's what I mean. I could write a method that handles this, but I'm sort of surprised that Laravel doesn't have anything to handle that?

Sinnbeck's avatar

@Darkdawg Why do you need a seperate file for each car? I would expect that the layout is the same and you just store the data in the database. Say you have a table called cars, and you have columns id | slug | name | description | image_path

Now you fill it up with all of your cars. And when you set up a route for a controller you want it to use the slug (eg. bmw)

Route::get('/cars/{car:slug}', ShowCarController::class)->name('car');

This allows the url to be /cars/bmw (if the slug is "bmw")

Then in that class you have something like this

class ShowCarController 
{
   public function __invoke(Car $car) {
        return view('car', [
                'car' => $car
          ]);
 
   }
 
}

You can now inside the car.blade.php write the code for how a car should be presented and use the car variable for each field

<h1>{{$car->name}}</h1>

Oh and finally.. In your regular page you can easily link to them

@foreach ($cars as $car)
    <a href="{{route('car', $car)}}">{{$car->name}}</a>
@endforeach
Darkdawg's avatar

@Sinnbeck Absolutely, I don't disagree. But it totally depends on the size and simplicity of the project, and the content in mind. Cars might have been a bad example, but what about something content-heavy, like insurance?

You would skip the need for an admin section and layout, auth, editor, etc. You get the advantage of version control and easy deployments, no sync issues between dev and prod, easy access to PHP features for content... but you lose the advantages of everything being data.

views/insurance/dog-insurance.blade.php
<x-layouts.insurance>
    <x-slot name="name">Dog insurance</x-slot>
    <x-slot name="image">dog-insurance.jpg</x-slot>
	This is the content for dog insurance...
</x-layouts.insurance>

I'm not sure all content needs to be in a database 🤔

Sinnbeck's avatar

@Darkdawg if you don't need to pass anything to the view and just want all content inside the view, you can just do

Route::view('/cars/bmw', 'bmw') //second parameter is the view 

Please or to participate in this conversation.