SortGo back

Understanding Laravel Folio: A Guide for Beginners and Intermediate Developers

/* by Ajay Patel - Jul 25, 2023 */

card image

Laravel Folio, introduced at LaraconUS 2023, is a new automatic file and directory-based routing system for Laravel. This feature allows developers to map a route to a specific file or folder, similar to file-based routing systems in frameworks like NextJS or NuxtJS.

How Laravel Folio Works

After installing Folio into your Laravel application, you can create a new page using the following artisan command: php artisan make:folio index. This command will create a new file inside a new pages directory located at resources/views/pages/index.blade.php. Now, when you visit your application in the browser, it will load this template as the homepage.

Wildcard Pages

Laravel Folio also supports wildcards in your page names, allowing for dynamic routing. For example, if you wanted to visit a route that looks like /users/1, you could create a file named [id].blade.php inside of a users folder. Inside the file, you will now have access to $id which can be used to lookup the user.

You can also take advantage of route model binding by changing the file name to use the Model, like so: /users/[User].blade.php. This will automatically load the User with the id passed.

Wildcard Directories

Folio also allows you to bind models to a directory. For instance, if you wanted to create an info page for every user, you could create a dynamic URL that looks like: /users/taylorotwell/info, by creating the following folder structure: resources/views/pages/users/[User:username]/info.blade.php.

Folio Middleware

You can also take advantage of Middleware with Page-based routing. For example, you can add a new Policy inside of the app/Policies directory, and then inside the view, you can add the following code:

```


{{ $post->title }}

{!! $post->body !!}
```

If the user does not have access, they will see a 403 Forbidden code; otherwise, they'll be able to view the post.

Conclusion

Laravel Folio provides a powerful and flexible way to handle routing in your Laravel application. It allows for a mix of Page-based routes and traditional routes within your application. Laravel Folio is scheduled to be released next week, and it's a feature that promises to bring a lot of value to the Laravel community.