Laravel Folio: Bring File-Based Routing to Your Application
Laravel Folio, introduced at LaraconUS 2023, is a new automatic file and directory-based routing system for Laravel. This feature allows you to map a route to a specific file or folder, similar to file-based routing systems in frameworks like Next.js or Nuxt.js. As of July 2023, Folio provides a powerful alternative to traditional explicit routing.
How Laravel Folio Works#
After installing Folio into your Laravel application, you can create a new page using a simple artisan command. This command creates a new file inside the pages directory where your routes live automatically.
php artisan make:folio index This command creates a new file at resources/views/pages/index.blade.php. When you visit your application in the browser, this template loads automatically as your homepage. Folio handles the routing logic for you, transforming your file structure into accessible routes without manual route registration.
Wildcard Pages and Dynamic Routing#
Laravel Folio supports wildcards in your page names, allowing you to create dynamic routes easily. You can create pages that respond to parameterized URLs by using square bracket notation in your file names. This combines well with reactive components like Laravel Livewire for building interactive pages.
For example, create a file named [id].blade.php inside a users folder to handle routes like /users/1. Inside your page file, you have access to the $id variable, which you can use to look up the user or fetch related data.
You can also take advantage of route model binding by naming your file to use the Model directly. This approach automatically loads the User with the id passed in the URL.
/users/[User].blade.php Wildcard Directories#
Folio also allows you to bind models at the directory level. You can create complex, nested dynamic URL structures by using wildcards in folder names. This is useful when you want to create an info page for every user or organize your pages by a resource parameter.
For instance, create a dynamic URL structure like /users/taylorotwell/info by organizing your folder structure like this:
resources/views/pages/users/[User:username]/info.blade.php This approach keeps your pages organized while maintaining dynamic routing at the directory level. The parameters become available to your page and any nested pages.
Middleware and Access Control#
You can take advantage of Laravel middleware and policies directly within your Folio pages. Instead of applying middleware through route definitions, use the @can directive to protect pages based on user permissions.
@can('view', $post)
{{ $post->title }}
{!! $post->body !!}
@endcan If the user does not have access to view the post, they will see a 403 Forbidden response. Otherwise, they can view the post content. This approach keeps your access control logic close to where it is used, making your pages more self-contained and easier to understand.
When to Use Laravel Folio#
Laravel Folio excels in scenarios where file-based routing simplifies your application structure. Use it when building content-heavy applications, dashboard pages, or any project where your page structure mirrors your file structure. For SaaS applications, Folio can significantly reduce the boilerplate needed to organize complex page hierarchies.
- Single-page applications or content sites with many pages
- Admin dashboards with nested resource pages
- Marketing sites with multiple landing pages
- Applications where file structure should match routes
- Projects migrating from Next.js or Nuxt.js
Building with Folio#
Laravel Folio provides a powerful and flexible way to handle routing in your Laravel application. It allows you to mix Page-based routes with traditional routes within your application, giving you the best of both worlds. When you need file-based routing, Folio delivers it as designed. For more complex routing scenarios or SaaS development, you can still rely on traditional Laravel routing alongside Folio.
If you are building a Laravel application that would benefit from file-based routing, Folio is a feature worth exploring. Learn more about integrating it into your workflow with maintenance and support planning.