Laravel Folio is a new automatic file and directory-based routing system. This gives you a "pages" directory; when one of those pages is hit from the URL, it automatically loads. Here is a preview of what Taylor demoed at Laracon.
Update: Laravel Folio beta is now released.
To use this feature, tell Laravel where your pages are located by defining them in the boostrap/app.php
file:
return Application::configure() ->withRouting( web: __DIR__.'/../routes/web.php', commands: __DIR__.'/../routes/console.php', pages: __DIR__.'/../resources/views/pages', )
For example, if you have a static site with an index page and an about page, then your URLs would look like this:
- site.com
- site.com/about
Those would then map to an index.blade.php file and an about.blade.php file.
Laravel Folio also supports wildcards in your page names. This allows for dynamic routing. For example:
users/[id].blade.php
You can then use the id at the top of the page to pull in the model from the db directly in Blade.
@php$user = /App/Models/User::findOrFail($id);@endphp
But you can also take advantage of route model binding by changing the file name to use the Model:
/users/[User].blade.php
Then it just knows to load the User with the id passed. Pretty powerful!
But wait, there's more...
You can also bind models to a directory like this:
/talks/[Talk]/feedback.php
This would map to site.com/talks/1/feedback
and automatically load the Talk model behind the scenes using FindOrFail
.
Eric is the creator of Laravel News and has been covering Laravel since 2012.