Giao diện
Bộ khởi tạo (Starter Kits)
Giới thiệu (Introduction)
Để giúp bạn khởi đầu nhanh việc xây dựng ứng dụng Laravel mới, chúng tôi cung cấp application starter kits. Các starter kits bao gồm routes, controllers và views cần thiết để đăng ký và xác thực người dùng. Starter kits sử dụng Laravel Fortify để xử lý authentication.
Dù bạn được khuyến khích sử dụng starter kits, chúng không bắt buộc. Bạn hoàn toàn tự do xây dựng ứng dụng từ đầu bằng cách cài bản Laravel mới.
Tạo ứng dụng với Starter Kit (Creating an Application)
Để tạo ứng dụng Laravel mới sử dụng starter kit, trước tiên bạn cần cài đặt PHP và Laravel CLI. Nếu đã có PHP và Composer, cài Laravel installer qua Composer:
bash
composer global require laravel/installerSau đó, tạo ứng dụng mới. Laravel installer sẽ yêu cầu bạn chọn starter kit:
bash
laravel new my-appSau khi tạo, bạn chỉ cần cài frontend dependencies và khởi động development server:
bash
cd my-app
npm install && npm run build
composer run devỨng dụng sẽ truy cập được tại http://localhost:8000.
Các Starter Kit có sẵn (Available Starter Kits)
React
Starter kit React cung cấp điểm khởi đầu mạnh mẽ, hiện đại để xây dựng ứng dụng Laravel với frontend React sử dụng Inertia. Inertia cho phép xây dựng ứng dụng React single-page hiện đại sử dụng server-side routing và controllers truyền thống — kết hợp sức mạnh frontend của React với năng suất backend của Laravel và biên dịch Vite cực nhanh.
Starter kit React sử dụng React 19, TypeScript, Tailwind và thư viện component shadcn/ui.
Svelte
Starter kit Svelte cung cấp điểm khởi đầu mạnh mẽ với frontend Svelte sử dụng Inertia.
Starter kit Svelte sử dụng Svelte 5, TypeScript, Tailwind và thư viện component shadcn-svelte.
Vue
Starter kit Vue cung cấp điểm khởi đầu tuyệt vời với frontend Vue sử dụng Inertia.
Starter kit Vue sử dụng Vue Composition API, TypeScript, Tailwind và thư viện component shadcn-vue.
Livewire
Starter kit Livewire cung cấp điểm khởi đầu hoàn hảo với frontend Laravel Livewire. Livewire là cách mạnh mẽ để xây dựng UI động, reactive chỉ bằng PHP — phù hợp cho teams chủ yếu sử dụng Blade templates và tìm kiếm giải pháp đơn giản hơn so với SPA frameworks.
Starter kit Livewire sử dụng Livewire, Tailwind và thư viện component Flux UI.
Tùy chỉnh Starter Kit (Starter Kit Customization)
Mọi starter kit cung cấp cho bạn quyền sở hữu đầy đủ code — bạn có thể điều chỉnh, tùy chỉnh và xây dựng ứng dụng theo cách bạn muốn.
Authentication
Tất cả starter kits sử dụng Laravel Fortify để xử lý authentication. Fortify cung cấp routes, controllers và logic cho login, registration, password reset, email verification và nhiều hơn.
Fortify tự động đăng ký các routes authentication sau dựa trên các tính năng được bật trong file cấu hình config/fortify.php:
| Route | Method |
|---|---|
/login | GET, POST |
/logout | POST |
/register | GET, POST |
/forgot-password | GET, POST |
/reset-password/{token} | GET |
/reset-password | POST |
/email/verify | GET |
/email/verify/{id}/{hash} | GET |
/email/verification-notification | POST |
/user/confirm-password | GET, POST |
/two-factor-challenge | GET, POST |
Sử dụng lệnh php artisan route:list để hiển thị tất cả routes trong ứng dụng.
Bật/tắt tính năng (Enabling and Disabling Features)
Bạn có thể kiểm soát tính năng Fortify nào được bật trong file config/fortify.php:
php
use Laravel\Fortify\Features;
'features' => [
Features::registration(),
Features::resetPasswords(),
Features::emailVerification(),
Features::twoFactorAuthentication([
'confirm' => true,
'confirmPassword' => true,
]),
],Để tắt một tính năng, comment hoặc xóa entry đó khỏi mảng features.
CẢNH BÁO
Khi sử dụng starter kit React, Svelte hoặc Vue, bạn cũng cần xóa các tham chiếu đến routes của tính năng đã tắt trong frontend code. Điều này cần thiết vì các starter kits sử dụng Wayfinder cho type-safe routing, tạo route definitions lúc build.
Tùy chỉnh tạo User và Reset Password
Khi user đăng ký hoặc reset password, Fortify gọi action classes trong thư mục app/Actions/Fortify:
CreateNewUser.php— Logic tạo user mớiResetUserPassword.php— Logic reset passwordPasswordValidationRules.php— Quy tắc validation password
Ví dụ tùy chỉnh logic đăng ký:
php
public function create(array $input): User
{
Validator::make($input, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'email', 'max:255', 'unique:users'],
'phone' => ['required', 'string', 'max:20'],
'password' => $this->passwordRules(),
])->validate();
return User::create([
'name' => $input['name'],
'email' => $input['email'],
'phone' => $input['phone'],
'password' => Hash::make($input['password']),
]);
}Xác thực hai yếu tố (Two-Factor Authentication)
Starter kits tích hợp sẵn xác thực hai yếu tố (2FA), cho phép người dùng bảo mật tài khoản bằng bất kỳ ứng dụng authenticator tương thích TOTP nào. 2FA được bật mặc định qua Features::twoFactorAuthentication() trong config/fortify.php.
Tùy chọn confirm yêu cầu user xác minh code trước khi 2FA được bật hoàn toàn, trong khi confirmPassword yêu cầu xác nhận password trước khi bật/tắt 2FA.
Giới hạn tần suất (Rate Limiting)
Rate limiting ngăn chặn brute-force và lặp lại đăng nhập làm quá tải authentication endpoints. Bạn có thể tùy chỉnh trong FortifyServiceProvider:
php
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Cache\RateLimiting\Limit;
RateLimiter::for('login', function ($request) {
return Limit::perMinute(5)->by($request->email.$request->ip());
});WorkOS AuthKit Authentication
Mặc định, các starter kits sử dụng hệ thống authentication tích hợp của Laravel. Ngoài ra, chúng tôi cũng cung cấp biến thể WorkOS AuthKit cung cấp:
- Social authentication (Google, Microsoft, GitHub và Apple)
- Passkey authentication
- Email "Magic Auth"
- SSO
Sử dụng WorkOS làm nhà cung cấp authentication yêu cầu tài khoản WorkOS. WorkOS cung cấp authentication miễn phí cho ứng dụng đến 1 triệu monthly active users.
Để sử dụng WorkOS AuthKit, chọn tùy chọn WorkOS khi tạo ứng dụng mới qua laravel new.
Cấu hình WorkOS Starter Kit
Sau khi tạo ứng dụng, thiết lập biến môi trường trong file .env:
ini
WORKOS_CLIENT_ID=your-client-id
WORKOS_API_KEY=your-api-key
WORKOS_REDIRECT_URL="${APP_URL}/authenticate"Inertia SSR
Starter kits React, Svelte và Vue tương thích với server-side rendering của Inertia. Để build SSR bundle:
bash
npm run build:ssrĐể tiện, lệnh composer dev:ssr cũng có sẵn — khởi động cả Laravel development server và Inertia SSR server.
Starter Kits do cộng đồng duy trì
Khi tạo ứng dụng mới, bạn có thể sử dụng bất kỳ starter kit cộng đồng nào trên Packagist qua cờ --using:
bash
laravel new my-app --using=example/starter-kitCâu hỏi thường gặp (FAQs)
Làm sao để nâng cấp?
Mỗi starter kit cung cấp điểm khởi đầu vững chắc với quyền sở hữu đầy đủ code. Không cần cập nhật starter kit itself.
Làm sao để bật email verification?
Bỏ comment import MustVerifyEmail trong model App/Models/User.php và đảm bảo model implement interface MustVerifyEmail:
php
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
class User extends Authenticatable implements MustVerifyEmail
{
// ...
}Sau đó thêm middleware verified vào routes cần bảo vệ:
php
Route::middleware(['auth', 'verified'])->group(function () {
Route::get('dashboard', function () {
return Inertia::render('dashboard');
})->name('dashboard');
});Làm sao để tùy chỉnh template email mặc định?
Xuất bản email views bằng lệnh:
bash
php artisan vendor:publish --tag=laravel-mailCác file sẽ được tạo trong resources/views/vendor/mail. Bạn có thể sửa đổi các file này cùng resources/views/vendor/mail/themes/default.css để thay đổi giao diện template email.