Skip to content

Laravel Folio

Nguồn gốc: Bản dịch từ Laravel Folio

Giới thiệu (Introduction)

Laravel Folio là page-based router mạnh mẽ, đơn giản hóa routing trong ứng dụng Laravel. Với Folio, tạo route dễ như tạo Blade template trong thư mục resources/views/pages.

Ví dụ, tạo trang truy cập tại /greeting — chỉ cần tạo file greeting.blade.php:

blade
<div>
    Hello World
</div>

Cài đặt (Installation)

bash
composer require laravel/folio

Chạy installer:

bash
php artisan folio:install

Page Paths / URIs

Mặc định, Folio serve trang từ resources/views/pages. Tùy chỉnh nhiều đường dẫn:

php
use Laravel\Folio\Folio;

Folio::path(resource_path('views/pages/guest'))->uri('/');

Folio::path(resource_path('views/pages/admin'))
    ->uri('/admin')
    ->middleware([
        '*' => [
            'auth',
            'verified',
        ],
    ]);

Subdomain Routing

Route theo subdomain:

php
use Laravel\Folio\Folio;

Folio::domain('admin.example.com')
    ->path(resource_path('views/pages/admin'));

Capture subdomain parameters:

php
Folio::domain('{account}.example.com')
    ->path(resource_path('views/pages/admin'));

Tạo Routes (Creating Routes)

Đặt Blade template vào thư mục mount của Folio → truy cập ngay qua browser.

bash
php artisan folio:list

Nested Routes

Tạo thư mục con cho nested route.

Index Routes

File index.blade.php trở thành "index" của thư mục chứa nó.

Route Parameters

Sử dụng ngoặc vuông trong tên file để capture URL segment:

bash
php artisan folio:page "users/[id]"

# pages/users/[id].blade.php → /users/1

Truy cập trong template:

blade
<div>
    User {{ $id }}
</div>

Capture nhiều segment:

bash
php artisan folio:page "users/[...ids]"

# pages/users/[...ids].blade.php → /users/1/2/3

Route Model Binding

Nếu wildcard segment tương ứng Eloquent model, Folio tự động inject model:

bash
php artisan folio:page "users/[User]"

# pages/users/[User].blade.php → /users/1
blade
<div>
    User {{ $user->id }}
</div>

Tùy chỉnh Key

Dùng cột khác id — chỉ định trong tên file:

[Post:slug].blade.php

Soft Deleted Models

Hỗ trợ truy xuất model đã soft delete.

Render Hooks

Thực thi logic trước khi render trang.

Named Routes

Đặt tên cho Folio route.

Middleware

Áp dụng middleware cho Folio page.

Route Caching

Folio hỗ trợ route caching cho hiệu năng production.