Laravel Folio: Streamlining File-Based Routing in Laravel
Laravel Folio is a powerful, elegant, and incredibly simple file-based router for Laravel applications. Introduced with Laravel 10, Folio allows you to define your application's routes by simply creating Blade templates within a designated directory, typically resources/views/pages. This paradigm significantly reduces the boilerplate associated with traditional web.php routing, making it perfect for content-heavy sites, landing pages, or when you prefer a more intuitive, file-system-driven approach to your application's structure.
Why Use Laravel Folio? #
- Simplicity: Define routes by creating files. No need to manage a large
web.phpfile for every page. - Intuitive Structure: Your URL structure directly mirrors your file system.
- Rapid Prototyping: Quickly spin up new pages without touching your route files.
- Cohesion: Keep your views and their corresponding routes tightly coupled.
Getting Started with Laravel Folio #
1. Installation #
Folio is a first-party Laravel package. You can install it via Composer:
composer require laravel/folio
After installation, publish Folio's configuration and create the default pages directory using the folio:install Artisan command:
php artisan folio:install
This command will create a resources/views/pages directory. This is where all your Folio-managed Blade templates will reside.
2. Creating Your First Page #
To create a simple "About Us" page, just create a Blade file within the pages directory:
touch resources/views/pages/about.blade.php
Now, add some content to it:
<!-- resources/views/pages/about.blade.php -->
<h1>About Us</h1>
<p>This is the about page.</p>
Navigate to /about in your browser, and you'll see your page!
Similarly, for your homepage, create resources/views/pages/index.blade.php.
3. Route Parameters #
Folio supports dynamic route parameters, much like traditional Laravel routes. You define them using square brackets [] in your file names.
Basic Parameters
To create a page that displays a post by its ID, create a file named [id].blade.php inside a posts directory:
touch resources/views/pages/posts/[id].blade.php
Inside resources/views/pages/posts/[id].blade.php:
<!-- resources/views/pages/posts/[id].blade.php -->
<h1>Post ID: {{ $id }}</h1>
<p>This is the content for post number {{ $id }}.</p>
Now, visiting /posts/1 will display "Post ID: 1", and /posts/50 will show "Post ID: 50".
Model Binding
Folio seamlessly integrates with Laravel's route model binding. If your parameter name matches a model, Folio will automatically attempt to retrieve the corresponding model instance.
Let's assume you have a User model. Create resources/views/pages/users/[User].blade.php:
touch resources/views/pages/users/[User].blade.php
Inside resources/views/pages/users/[User].blade.php:
<!-- resources/views/pages/users/[User].blade.php -->
<h1>User Profile: {{ $user->name }}</h1>
<p>Email: {{ $user->email }}</p>
Now, visiting /users/1 (assuming a user with ID 1 exists) will automatically inject the User model instance into your Blade template. Folio supports implicit model binding and custom keys just like web.php routes.
4. Nested Routes #
Organizing your routes into subdirectories automatically creates nested URLs.
For an admin dashboard, you might create:
mkdir -p resources/views/pages/admin
touch resources/views/pages/admin/dashboard.blade.php
touch resources/views/pages/admin/settings.blade.php
Now, /admin/dashboard and /admin/settings will serve these respective pages.
5. Layouts #
Folio allows you to define layouts for groups of pages. You can define a _layout.blade.php file within any directory, and all pages within that directory and its subdirectories will automatically use that layout.
For example, to create a layout for all admin pages:
touch resources/views/pages/admin/_layout.blade.php
Inside resources/views/pages/admin/_layout.blade.php:
<!-- resources/views/pages/admin/_layout.blade.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<title>Admin Area</title>
</head>
<body>
<nav>
<a href="/admin/dashboard">Dashboard</a> |
<a href="/admin/settings">Settings</a>
</nav>
<div class="content">
{{ $slot }}
</div>
</body>
</html>
Now, resources/views/pages/admin/dashboard.blade.php content will be rendered into the $slot variable of this layout. You don't need to use @extends or @section.
You can also use a global layout by placing _layout.blade.php directly in resources/views/pages.
6. Middleware #
You can attach middleware to Folio pages directly within the Blade file using the @php folio() directive.
For example, to protect an admin page:
<!-- resources/views/pages/admin/dashboard.blade.php -->
@php folio()->middleware(['auth', 'admin']) @endphp
<h1>Admin Dashboard</h1>
<p>Welcome, {{ auth()->user()->name }}!</p>
This will apply the auth and a hypothetical admin middleware to the /admin/dashboard route.
Conclusion #
Laravel Folio offers a refreshing approach to routing, especially for applications where the page structure closely mirrors the URL structure. It simplifies development, enhances readability, and provides a clean separation of concerns for your frontend pages. While it might not replace web.php entirely for complex API routes or highly dynamic scenarios, it's an excellent tool to have in your Laravel arsenal for building elegant and maintainable web applications. Give it a try and experience the simplicity!