1.Laravel是什麼?
從哲學上講,Laravel 是最受歡迎的 PHP 框架 “優雅的語法” — 程式碼乾淨、易於閱讀,開發人員體驗至上。 Laravel 提供了完整的生態系統:ORM(Eloquent)、佇列、快取、身份驗證、測試、部署。
| 框架 | 語言 | ORM | 優點 |
|---|---|---|---|
| 拉維爾 | PHP | 雄辯 | 大生態系統,大DX |
| 交響樂團 | PHP | 教義 | 企業化、靈活化 |
| 薑戈 | 蟒蛇 | Django ORM | 含電池,內建管理 |
| 春季啟動 | 爪哇 | JPA/休眠 | 企業、微服務 |
2.PHP 8.4 要點
<?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. 安裝 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. 資料夾結構
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
下一篇: 路由、控制器和中介軟體。