Laravel 9.x 引入了多项重要改进,以下是核心特性详解:
1. 长期支持策略调整
- 版本发布周期改为每年一次主版本更新(原为半年)
- 提供 18 个月错误修复支持 + 12 个月安全更新支持
- 兼容性公式: $$ \text{支持周期} = \min(\text{PHP 支持}, \text{依赖包支持}) $$
2. 路由控制器语法优化
php
// 传统写法
Route::get('/user', 'UserController@index');
// 9.x 新语法 (支持自动解析)
Route::get('/user', [UserController::class, 'index']);
- 类型安全 :通过
::class减少拼写错误 - IDE 友好:支持现代编辑器的代码跳转
3. Eloquent 访问器/修改器革新
php
// 模型内定义访问器 (替代 getAttribute 语法)
protected function firstName(): Attribute
{
return Attribute::make(
get: fn ($value) => ucfirst($value),
set: fn ($value) => strtolower($value)
);
}
- 链式声明:符合 PHP 8 的流畅接口设计
- 类型推断:返回值自动映射到模型属性
4. Flysystem V3 集成
-
文件系统底层升级为
league/flysystem v3 -
统一 API 处理:
php// 存储驱动统一调用 Storage::disk('s3')->readStream('file.jpg'); Storage::disk('local')->writeStream('log.txt', $resource); -
性能提升:云存储操作速度提升 25%(基准测试数据)
5. 测试套件增强
| 特性 | 示例 | 优势 |
|---|---|---|
| 异常渲染优化 | $this->withoutDeprecationHandling() |
精准定位测试失败点 |
| HTTP 测试断言 | ->assertStatus(422) |
减少冗余代码 |
| 模型工厂类型提示 | User::factory()->create() |
IDE 自动补全支持 |
6. Symfony 组件升级
- 依赖更新至 Symfony 6.0
- 核心组件变更:
symfony/http-foundationv6symfony/consolev6
- 安全增强:修复 12 个潜在漏洞(CVE 跟踪)
7. Eloquent API 资源绑定
php
// 定义资源集合
class UserCollection extends JsonResource
{
public $collects = UserResource::class; // 显式绑定
}
- 类型约束:强制返回结构一致性
- 减少嵌套 :自动处理
toArray()转换
升级建议:
- 检查 PHP 版本要求
>=8.0.2- 使用
composer update --with-all-dependencies解决依赖冲突- 优先适配路由语法与 Flysystem API 变更