Full-Stack Developer & Creator of Dasun DEV

Laravel Folio

Laravel Folio

Introduction

Routing is a fundamental aspect of web development, and for Laravel developers, managing routes efficiently is crucial. Laravel Folio is a powerful page-based router designed to simplify routing in Laravel applications. By integrating Folio into your project, creating and managing routes becomes a breeze, enhancing productivity and maintainability.

Installation

To get started with Laravel Folio, install it into your project using Composer, the popular package manager for PHP:

composer require laravel/folio:^1.0@beta

Once installed, run th Artisan command:

php artisan folio:install

Let's dive right in! 💦🏊‍♂️

Creating Routes

One of Laravel Folio's key advantages is its straightforward approach to creating routes. Instead of defining routes in separate configuration files, Folio utilizes Blade templates within the resources/views/pages directory to specify routes.

For instance, to create a page accessible at /greeting, simply create a greeting.blade.php file inside the resources/views/pages directory:

<div>
    Hello World
</div>

Once the template is in place, the route becomes accessible via the browser at http://example.com/greeting.

Nested Routes

Folio also supports nested routes, allowing you to create routes within subdirectories. For example, to have a page accessible at /user/profile, create a profile.blade.php template within the pages/user directory:

php artisan make:folio user/profile
# pages/user/profile.blade.php → /user/profile

Index Routes

Index routes are useful when you want to set a specific page as the "index" of a directory. To achieve this, place an index.blade.php template within a Folio directory. Any requests to the root of that directory will be routed to the index page.

For instance:

php artisan make:folio index
# pages/index.blade.php → /

php artisan make:folio users/index
# pages/users/index.blade.php → /users

Route Parameters

Folio provides a convenient way to capture segments of the incoming request's URL and use them as variables in your page's template. Encapsulate a segment of the page's filename in square brackets to achieve this:

php artisan make:folio "users/[id]"
# pages/users/[id].blade.php → /users/1

To capture multiple segments, use three dots ... as a prefix:

php artisan make:folio "users/[...ids]"
# pages/users/[...ids].blade.php → /users/1/2/3

Route Model Binding

Folio seamlessly integrates with Laravel's route model binding. If a wildcard segment of your page template's filename corresponds to one of your application's Eloquent models, Folio automatically attempts to inject the resolved model instance into your page:

php artisan make:folio users/[User]
# pages/users/[User].blade.php → /users/1

Customizing The Key

You can customize how Folio resolves bound Eloquent models by specifying a column other than the default id. Include the desired column in the page's filename:

[Post:slug].blade.php

This will attempt to resolve the bound model via the slug column instead of the id column.

Model Location

By default, Folio looks for your models within the app/Models directory. However, you can specify a fully-qualified model class name in your template's filename if needed:

php artisan make:folio "users/[.App.Models.User]"
# pages/users/[.App.Models.User].blade.php → /users/1

Soft Deleted Models

Folio excludes soft-deleted models by default when resolving implicit model bindings. But you can instruct Folio to retrieve soft-deleted models by using the withTrashed function within the page's template:

<?php
use function Laravel\Folio\{withTrashed};
withTrashed();
?>
<div>
    User {{ $user->id }}
</div>

Middleware

Middleware is crucial for controlling access and adding functionality to your routes. Folio allows you to apply middleware to specific pages or groups of pages. You can either invoke the middleware function within a page's template or provide the middleware argument when invoking the Folio::route method.

You can even include closures in the array of middleware to define inline, anonymous middleware for fine-grained control over page-specific functionality.

PHP Blocks

When using Folio, be mindful that <?php and ?> tags are reserved for Folio page definition functions like middleware and withTrashed. For regular PHP code within your Blade templates, use the @php Blade directive:

@php
    if (! Auth::user()->can('view-posts', $user)) {
        abort(403);
    }

    $posts = $user->posts;
@endphp

@foreach ($posts as $post)
    <div>
        {{ $post->title }}
    </div>
@endforeach

Conclusion

Laravel Folio streamlines routing in Laravel applications, offering an elegant and intuitive way to manage routes using Blade templates. Its support for nested routes, route parameters, route model binding, and middleware empowers developers to create feature-rich applications with ease. With Laravel Folio, you can take your Laravel project to new heights of productivity and maintainability.

No comments yet…