Laravel 5.0 為框架發展歷程中的里程碑版本,帶來全新目錄結構、Contracts 介面體系、路由快取、中介層、依賴注入增強、認證腳手架、事件物件、命令總線、排程任務等革命性特性。本文深度解析各項核心更新與遷移指南。
app/models,程式碼直接置於 app/,預設命名空間 Appphp artisan app:name NewNamespace 修改命名空間app/Http:Controllers、Middleware、Requestsapp/Providers 取代 app/start,服務提供者管理啟動流程resources/全面採用 PSR-4 標準,Composer autoload 設定:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-4": {
"App\\": "app/"
}
}
所有核心元件實作 illuminate/contracts 定義的介面:
// 透過 Contract 注入,而非具體類別
use Illuminate\Contracts\Auth\Registrar;
class AuthController {
public function __construct(Registrar $registrar) {
$this->registrar = $registrar;
}
}
php artisan route:cache # 建立快取
php artisan route:clear # 清除快取
auth、csrf 等內建功能皆轉為 Middleware$next($request) 前後)php artisan make:middleware CheckAge
// app/Http/Middleware/CheckAge.php
public function handle($request, Closure $next) {
if ($request->age < 18) {
return redirect('home');
}
return $next($request);
}
// 路由註冊
Route::get('profile', ['middleware' => 'age', function () {
// 只有通過檢查才能到達
}]);
除了建構子注入,現在可直接在方法簽名型別提示依賴:
public function createPost(Request $request, PostRepository $posts) {
// IoC 容器自動解析注入
// 即使路由包含其他參數也能正常運作
}
resources/views/auth/database/migrations/xxxx_create_users_table.phpauth/login、auth/registerApp\Services\Auth\Registrar 處理建立/驗證邏輯php artisan make:auth # 5.2+ 版本指令
// 定義事件物件
class PodcastWasPurchased {
public $podcast;
public function __construct(Podcast $podcast) {
$this->podcast = $podcast;
}
}
// 觸發事件
Event::fire(new PodcastWasPurchased($podcast));
// 監聽器接收物件
class ReportPodcastPurchase {
public function handle(PodcastWasPurchased $event) {
// $event->podcast 直接存取
}
}
class PurchasePodcast extends Command implements SelfHandling, ShouldBeQueued {
use SerializesModels;
protected $user, $podcast;
public function __construct(User $user, Podcast $podcast) {
$this->user = $user;
$this->podcast = $podcast;
}
public function handle() {
// 業務邏輯
event(new PodcastWasPurchased($this->user, $this->podcast));
}
}
// 同步執行
$this->dispatch(new PurchasePodcast($user, $podcast));
// 非同步佇列(需配置 queue worker)
// 相同程式碼,自動進入佇列
內建 database 佇列驅動,無需安裝 Redis/Beanstalkd 即可使用佇列功能。
// app/Console/Kernel.php
protected function schedule(Schedule $schedule) {
$schedule->command('artisan:command')->dailyAt('15:00');
$schedule->call(function () { /* ... */ })->hourly();
$schedule->exec('script.sh')->weekly();
}
* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
| 特性 | 說明 |
|---|---|
| Tinker / Psysh | 替代 Boris,完整支援 Windows,php artisan tinker |
| DotEnv | 環境配置管理,替代巢狀環境設定 |
| Laravel Elixir | Gulp 封裝,編譯 Less/Sass/CoffeeScript,執行測試 |
| Laravel Socialite | OAuth 套件,支援 Facebook/Twitter/Google/GitHub |
| Flysystem 整合 | 統一 API 操作本地/S3/Rackspace 儲存 |
| Form Request | 驗證邏輯封裝於專用類別,支援控制器注入自動驗證 |
| Simple Validation | 控制器 $this->validate() 輕量驗證 |
| Artisan 產生器 | 新增多項 make:* 指令 |
| Config Cache | php artisan config:cache 效能優化 |
| Symfony VarDumper | dd() 升級,彩色高亮、摺疊陣列 |
Laravel 5.0 不僅是版本號躍升,更是架構層面的重大重構。新目錄結構符合現代 PHP 標準,Contracts 體系奠定可擴展基礎,Middleware 統一請求處理,命令總線與排程任務解決長期痛點。對於新專案,建議直接採用 5.0+ 架構;現有 4.2 專案評估遷移成本後,可分階段升級。掌握這些核心特性,能充分發揮 Laravel 開發效能與維護性優勢。