Page based routing for Laravel.
A simple demo using Laravel Folio.
This page is being served by Laravel Folio.
Instead of defining a route in your web.php
file,
you simply create a file in the resources/views/pages
directory.
Hello from Laravel Folio!
A quick walkthrough of how this works.
Laravel Folio is a powerful page-based router designed to simplify routing in Laravel applications.
To get started, you need to install Folio via Composer:
1composer require laravel/folio
Then, you may run the folio:install
Artisan command, which will install Folio's service provider:
1php artisan folio:install
By default, Folio serves pages from your application's resources/views/pages
directory.
This is configured in the boot
method of your FolioServiceProvider
:
1<?php 2 3namespace App\Providers; 4 5use Illuminate\Support\ServiceProvider; 6use Laravel\Folio\Folio; 7 8class FolioServiceProvider extends ServiceProvider 9{10 /**11 * Bootstrap services.12 */13 public function boot(): void14 {15 Folio::path(resource_path('views/pages'));16 }17}
To create a route, just create a Blade file in the resources/views/pages
directory.
The URL for the page will automatically match the file path relative to this directory.
For example, this page is located at resources/views/pages/demos/folio.blade.php
, making it accessible at /demos/folio
.
1<?php 2use function Laravel\Folio\name; 3 4name('demos.folio'); 5?> 6 7<x-layouts.html 8 title="Laravel Folio - Demos" 9 pageTitle="Laravel Folio"10 pageTagLine="Page based routing for Laravel."11>12 <x-content-container class="w-full mb-8">13 <div class="mb-8 text-left">14 <h2 class="text-2xl mb-2">Demo</h2>15 <p class="mb-2 text-slate-500">A simple demo using Laravel Folio.</p>16 <x-gradent-under-line />17 </div>18 19 <div class="text-left">20 <x-content-text class="mb-8">21 This page is being served by Laravel Folio.22 Instead of defining a route in your <x-text.code>web.php</x-text.code> file,23 you simply create a file in the <x-text.code>resources/views/pages</x-text.code> directory.24 </x-content-text>25 26 <x-content-text class="mb-8 text-2xl text-center">27 Hello from Laravel Folio!28 </x-content-text>29 </div>30 </x-content-container>31 32 {{-- Show Your Working section will go here --}}33 34</x-layouts.html>
You can link to Folio pages using their URL path, the url()
helper, or by using named routes if they are defined:
1<!-- In your Blade templates, you can link to Folio pages using the URL path -->2<a href="/demos/folio">Go to Folio Demo</a>3 4<!-- Or using the url() helper -->5<a href="{{ url('/demos/folio') }}">Go to Folio Demo</a>
Folio allows you to define named routes for your pages using the name
function at the top of your Blade file. This makes it easier to link to pages without hardcoding URLs:
1<?php2use function Laravel\Folio\name;3 4name('demos.folio');5?>6 7<x-text.link href="{{ route('demos.folio') }}">8 Go to Folio Demo9</x-text.link>
Folio supports route parameters by using square brackets in the filename. For example, [id].blade.php
will capture the id
parameter. You can also use route model binding by using the model name:
1{{-- /resources/views/pages/users/[id].blade.php --}} 2 3<x-content-text> 4 User ID: {{ $id }} 5</x-content-text> 6 7{{-- Or using route model binding --}} 8{{-- /resources/views/pages/users/[User].blade.php --}} 9 10<x-content-text>11 User Email: {{ $user->email }}12</x-content-text>
You can apply middleware to individual Folio pages using the middleware
function. This is useful for protecting routes or applying global filters:
1<?php2use function Laravel\Folio\middleware;3 4middleware(['auth', 'verified']);5?>6 7<x-content-text>8 This page is restricted to logged-in and verified users.9</x-content-text>
A few links to supporting resources.