Mastering Laravel Folio: A Guide to Page-Based Routing
Overview
web.php to a file-based system. This approach maps your directory structure directly to your application's URLs. It is particularly effective for content-heavy sections like marketing pages, blogs, or documentation where creating a dedicated controller for every static view feels like overkill. By removing the middleman, you can build faster and keep your logic closer to the UI.
Prerequisites
To get the most out of this tutorial, you should be comfortable with the
Key Libraries & Tools
- Laravel Folio: The primary page-based router package.
- Blade: Laravel's powerful templating engine used for the page files.
- Artisan: Laravel's command-line interface for generating pages and managing the environment.
- Sushi: A flat-file database driver (optional, mentioned for markdown-based workflows).
Code Walkthrough
Installation
First, pull the package into your project and run the installation command to prepare your directories.
composer require laravel/folio
php artisan folio:install
Creating a Basic Page
Folio looks for files inside resources/views/pages. Any Blade file created here automatically becomes a route.
<!-- resources/views/pages/hi.blade.php -->
<div>
<h1>Hi there!</h1>
</div>
Navigating to /hi in your browser will now render this view instantly without any entry in web.php.
Route Model Binding
You can capture dynamic parameters or even perform Route Model Binding by naming your files with brackets.
# Creates a route at /users/{user}
php artisan folio:page "users/[User]"
Inside the file, you access the model directly:
@php
use function Laravel\Folio\name;
name('profile');
@endphp
<h1>User: {{ $user->name }}</h1>
Syntax Notes
Folio introduces specialized functions that live within @php blocks at the top of your Blade files. The name() function allows you to assign a route name for easy referencing via the route() helper. For security, the middleware() function lets you attach authentication or custom logic directly to the page file.
Practical Examples
- Marketing Sites: Rapidly deploy dozens of landing pages by simply dropping Blade files into the pages folder.
- Blogs: Combine Folio with a markdown parser to turn a directory of text files into a fully functional blog.
- Admin Dashboards: Use nested directories to create organized, resource-specific management views.
Tips & Gotchas
Always remember that Folio routes are additive; they do not override existing routes defined in your web.php unless you explicitly remove the conflicting definitions. If you are on a Windows environment, note that custom key binding syntax might require dashes instead of colons in filenames. For performance, use php artisan folio:list to visualize your route tree and ensure nested directories are resolving as expected.
