Laravel Starter Kit
Các starter kit của Laravel cung cấp scaffold sẵn dùng cho ứng dụng Inertia mới.Đây là cách nhanh nhất để bắt đầu dự án Inertia mới bằng Laravel và Vue hoặc React. Tuy nhiên, nếu muốn cài Inertia thủ công vào ứng dụng, hãy xem tài liệu bên dưới.
Cài đặt
1
Install dependencies
Trước tiên, hãy cài adapter phía máy chủ của Inertia bằng Composer package manager.
2
Setup root template
Tiếp theo, tạo file Với ứng dụng React, nên đặt directive Nếu thay
resources/views/app.blade.php. Root template này được tải ở lần truy cập page đầu tiên. Inertia cung cấp Blade component để render nội dung head và body.resources/views/app.blade.php
@viteReactRefresh trước @vite để bật Fast Refresh trong môi trường phát triển.Component <x-inertia::app> render phần tử <div> với id là app. Element này là mounting point cho JavaScript application. Bạn có thể tùy chỉnh id bằng cách truyền giá trị khác vào component.id của root element, hãy nhớ cập nhật cả phía client.Component <x-inertia::head> cũng nhận fallback content qua slot để quản lý head element khi SSR không hoạt động.Mặc định, Laravel adapter của Inertia giả định root template có tên app.blade.php. Bạn có thể thay đổi bằng phương thức Inertia::setRootView().3
Register middleware
Tiếp theo, thiết lập Inertia middleware bằng cách publish middleware Sau khi middleware được publish, hãy thêm middleware Middleware này cung cấp phương thức
HandleInertiaRequests vào ứng dụng bằng Artisan command sau.HandleInertiaRequests vào group middleware web trong file bootstrap/app.php của ứng dụng.version() để đặt asset version, cùng phương thức share() để định nghĩa shared data.Blade directives
Trước v3, Inertia dùng Blade directive@inertiaHead và @inertia để render nội dung head và body. Các directive này vẫn được hỗ trợ nhưng không cung cấp fallback content cho SSR head. Các Blade component ở trên được khuyến nghị cho ứng dụng mới.
resources/views/app.blade.php
id của root element bằng cách truyền giá trị vào directive: @inertia('custom-app-id').