Laravel 2.x 是框架早期的重要迭代版本(2010年发布),主要特性如下:
1. 路由系统增强
- 引入命名路由功能:
Route::get('profile', ['as' => 'profile', 'uses' => 'UserController@profile']) - 支持路由参数约束:
Route::get('user/{id}', function($id) {...})->where('id', '\d+')
2. 控制器改进
-
实现RESTful控制器基类:
phpclass UserController extends BaseController { public function getIndex() { /* 列表页 */ } public function postCreate() { /* 创建逻辑 */ } }
3. 视图层优化
-
支持视图继承和区块:
blade<!-- 父视图 --> @section('sidebar') 默认侧边栏 @show <!-- 子视图 --> @extends('layout') @section('sidebar') 自定义侧边栏内容 @endsection
4. 数据库迁移系统
-
引入迁移命令行工具:
bashphp artisan migrate:make create_users_table -
迁移文件示例:
phpSchema::create('users', function($table) { $table->increments('id'); $table->string('email')->unique(); });
5. 单元测试支持
-
集成PHPUnit测试框架
-
提供测试辅助方法:
phppublic function testLoginRedirect() { $response = $this->call('POST', '/login'); $this->assertRedirectedTo('/dashboard'); }
6. 命令行工具 (Artisan)
-
新增生成器命令:
bashphp artisan generate:controller php artisan generate:model
7. 扩展包系统
- 引入Bundle机制(现代Package的前身)
- 支持通过
bundles.php配置文件注册扩展
注意:Laravel 2.x 已于2012年停止维护,建议使用现代版本(如10.x)。其核心架构思想如路由、Eloquent ORM等概念在后续版本中持续演进完善。