1. Laravel là gì?
Laravel là PHP framework phổ biến nhất, theo triết lý "elegant syntax" — code sạch, dễ đọc, developer experience đặt lên hàng đầu. Laravel cung cấp hệ sinh thái hoàn chỉnh: ORM (Eloquent), queue, cache, auth, testing, deployment.
| Framework | Ngôn ngữ | ORM | Ưu điểm |
|---|---|---|---|
| Laravel | PHP | Eloquent | Hệ sinh thái lớn, DX tuyệt vời |
| Symfony | PHP | Doctrine | Enterprise, linh hoạt |
| Django | Python | Django ORM | Batteries-included, admin built-in |
| Spring Boot | Java | JPA/Hibernate | Enterprise, microservices |
2. PHP 8.4 Essentials
<?php
// Enums
enum UserRole: string {
case Admin = 'admin';
case Staff = 'staff';
case Customer = 'customer';
}
// Readonly properties
class Product {
public function __construct(
public readonly string $name,
public readonly float $price,
) {}
}
// Named arguments
$user = User::create(
name: 'John',
email: '[email protected]',
role: UserRole::Customer,
);
// Match expression
$label = match($status) {
'active' => 'Hoạt động',
'inactive' => 'Tạm dừng',
default => 'Không xác định',
};
3. Cài đặt Laravel
# Với Composer
composer create-project laravel/laravel my-app
# Với Laravel Installer
composer global require laravel/installer
laravel new my-app
# Với Laravel Sail (Docker)
curl -s "https://laravel.build/my-app?with=pgsql,redis" | bash
cd my-app && ./vendor/bin/sail up
4. Cấu trúc thư mục
my-app/
├── app/
│ ├── Http/Controllers/
│ ├── Models/
│ └── Providers/
├── config/
├── database/
│ ├── factories/
│ ├── migrations/
│ └── seeders/
├── resources/views/
├── routes/
│ ├── web.php
│ └── api.php
├── storage/
├── tests/
├── .env
└── artisan
# Artisan commands
php artisan serve # Dev server
php artisan make:model Product -mfc # Model + migration + factory + controller
php artisan migrate # Chạy migrations
php artisan tinker # REPL
Bài tiếp theo: Routing, Controllers & Middleware.