---
title: File based routing in Inertia.js
url: "https://aaronfrancis.com/writing/file-based-routing-in-inertiajs"
path: "/writing/file-based-routing-in-inertiajs"
type: technical_article
author: Aaron Francis
published: 2024-08-01
updated: 2026-11-10
topic: "Laravel & PHP"
description: Laravel + Inertia file-based routing for simple pages.
keywords: [inertia, file-based routing, laravel, vue, router macro, static pages, route constraint, appserviceprovider, wherein, default.vue]
site_index: "/llms.txt"
keyword_index: "/llms/keywords.txt"
---

# File based routing in Inertia.js

As a part of [automating my life](https://youtube.com/live/n4ulosX4fW0) I've been building out
an [Inertia.js](https://inertiajs.com/) frontend with a Laravel backend.

One thing I wanted was to have a very simplified routing structure for "static" pages, that is pages that don't need
props, controllers, or anything else. They're still Vue components, but with just static data.

On this site, for example, I have a [lists](/lists) page which contains my best lists of things. (They're very good lists.)

Here's what it looks like on the filesystem:

```text
resources
└─ js
   └─ Pages
      └─ Lists
         └─ Broken.vue    <-- static page
         └─ Default.vue   <-- static page
         └─ Scifi.vue     <-- static page
```

To make this easier, I added an `inertiaPages` macro to the router, so that I could route all of those lists in one
line:

```php
// web.php
Route::inertiaPages('/lists/{page}', resource_path('js/Pages/Lists'));
```

When anyone visits `/lists/scifi`, Inertia will serve the `Scifi.vue` page from the `js/Pages/Lists` directory.

The macro is relatively straightforward. You can place it in the `boot` method of your `AppServiceProvider`. We'll walk
through it step by step, but here it is in its entirety:

```php
namespace App\Providers;

use Illuminate\Routing\Router;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\File;
use Illuminate\Support\ServiceProvider;
use Inertia\Inertia;
use SplFileInfo;

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        Router::macro('inertiaPages', function ($uri, $directory) {
            $pages = array_map(function (SplFileInfo $file) {
                return Str::kebab($file->getBasename('.vue'));
            }, File::allFiles($directory));

            if ($default = Arr::where($pages, fn($value) => $value === 'default')) {
                Arr::pull($pages, head(array_keys($default)));

                $this->match(
                    ['GET', 'HEAD'],
                    Str::chopEnd($uri, '/{page}'),
                    fn() => Inertia::render('Lists/Default')
                );
            }

            return $this
                ->match(
                    ['GET', 'HEAD'],
                    $uri,
                    fn($page) => Inertia::render('Lists/' . ucfirst(Str::camel($page)))
                )
                ->whereIn('page', $pages);
        });
    }
}
```

The first few lines just register the macro in the router, receiving the URI pattern and the directory to look for the pages.

```php
Router::macro('inertiaPages', function ($uri, $directory) {
    //
});
```

Next, we grab all the files in the directory and get just their basenames, with no `.vue` extension. (Note, this doesn't
handle subdirectory routing. With a few tweaks it could!)

```php
Router::macro('inertiaPages', function ($uri, $directory) {
    $pages = array_map(function (SplFileInfo $file) {
        // Kebab is nice for urls. ScifiBooks.vue would become scifi-books
        return Str::kebab($file->getBasename('.vue'));
    }, File::allFiles($directory));
});
```

Each individual page lives at its own `/lists/{page}` url. To allow for a `/lists` url with no page, you can create a `Default.vue`
that will be served when there is no page param.

```php
Router::macro('inertiaPages', function ($uri, $directory) {
    $pages = array_map(function (SplFileInfo $file) {
        return Str::kebab($file->getBasename('.vue'));
    }, File::allFiles($directory));

    // See if there's a default page
    if ($default = Arr::where($pages, fn($value) => $value === 'default')) {
        // Remove it from the pages array so /lists/default isn't a valid route. 
        Arr::pull($pages, head(array_keys($default)));

        $this->match(
            ['GET', 'HEAD'],
            // Remove the page param from the uri, turning it into just /lists
            Str::chopEnd($uri, '/{page}'),
            fn() => Inertia::render('Lists/Default')
        );
    }
});
```

Finally, we create the route to match all the pages. Importantly, we *only* create routes that have valid pages. We use
[Laravel's `whereIn` route constraint](https://laravel.com/docs/11.x/routing) to ensure that that's the case.

```php
Router::macro('inertiaPages', function ($uri, $directory) {
    $pages = array_map(function (SplFileInfo $file) {
        return Str::kebab($file->getBasename('.vue'));
    }, File::allFiles($directory));

    if ($default = Arr::where($pages, fn($value) => $value === 'default')) {
        Arr::pull($pages, head(array_keys($default)));

        $this->match(
            ['GET', 'HEAD'],
            Str::chopEnd($uri, '/{page}'),
            fn() => Inertia::render('Lists/Default')
        );
    }

    return $this
        ->match(
            ['GET', 'HEAD'],
            $uri,
            // ucfirst + camel = PascalCase 
            fn($page) => Inertia::render('Lists/' . ucfirst(Str::camel($page)))
        )
        // Only match valid pages
        ->whereIn('page', $pages);
});
```

And that's it! Now you can add pages as you see fit and they'll be auto-wired.

Please let me know [on Twitter](https://twitter.com/aarondfrancis) if you liked it or have any ideas on how to make it better!

---

## Related on aaronfrancis.com

- [#33 - Aaron Francis on Fusion, TUIs and Content Creation](/podcast/33-aaron-francis-on-fusion-tuis-and-content-creation-087d6b18) *(Podcast · 2025-02-13 · shared: inertia, laravel, vue)*
- [This is a season of Type Two fun](/newsletter/this-is-a-season-of-type-two-fun) *(Newsletter · 2025-08-08 · shared: vue, laravel, inertia)*
- [Speed up your Inertia.js website with these 7 tactics](/videos/speed-up-your-inertiajs-website-with-these-7-tactics) *(Video · 2025-06-25 · shared: inertia, laravel, vue)*
- [Aaron Francis: Building and Teaching Cool Things](/podcast/aaron-francis-building-and-teaching-cool-things-39697cbb) *(Podcast · 2025-03-11 · shared: inertia, laravel, vue)*
- [Aaron Francis is putting PHP in Your JS Files](/podcast/aaron-francis-is-putting-php-in-your-js-files-6f3117a7) *(Podcast · 2025-03-05 · shared: laravel, inertia, vue)*

## Relevant Resources from Aaron Francis

- **[Solo](https://soloterm.com)** (overview on site: [/software](/software)): All-in-one agentic development platform and terminal workspace built by Aaron Francis for orchestrating processes and AI coding agents.

## Search aaronfrancis.com (2-Hop Static Keyword Index)

Pick the alphabetical shard matching any topic to find exact pages across all articles, newsletters, videos, talks, and podcast transcripts (every shard is < 40 KB):
[A-B](/llms/keywords/a-b.txt) · [C](/llms/keywords/c.txt) · [D-E](/llms/keywords/d-e.txt) · [F-G](/llms/keywords/f-g.txt) · [H-J](/llms/keywords/h-j.txt) · [K-L](/llms/keywords/k-l.txt) · [M-N](/llms/keywords/m-n.txt) · [O-P](/llms/keywords/o-p.txt) · [Q-R](/llms/keywords/q-r.txt) · [S](/llms/keywords/s.txt) · [T-Z](/llms/keywords/t-z.txt) · [Directory](/llms/keywords.txt) · [Site Map](/llms.txt)

Shards for this page's topics: `inertia` ([H-J](/llms/keywords/h-j.txt)), `file-based routing` ([F-G](/llms/keywords/f-g.txt)), `laravel` ([K-L](/llms/keywords/k-l.txt)), `vue` ([T-Z](/llms/keywords/t-z.txt)).

## About Aaron Francis (Current Context)

Aaron Francis is VP of Marketing and Community at [Laravel](https://laravel.com), founder of [Try Hard Studios](https://tryhardstudios.com), creator of [Solo](https://soloterm.com), co-host of [Mostly Technical](/podcast), and author of [Mastering Postgres](/courses), [High Performance SQLite](/courses), [MySQL for Developers](/courses), and [Screencasting.com](/courses). Full bio: [/about](/about) · Current focus: [/now](/now).
