Giao diện
Laravel Folio
- Giới thiệu
- Cài đặt
- Tạo Routes
- Route Parameters
- Route Model Binding
- Render Hooks
- Named Routes
- Middleware
- Route Caching
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/folioChạy installer:
bash
php artisan folio:installPage 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:listNested 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/1Truy 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/3Route 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/1blade
<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.phpSoft 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.