好的,Laravel 6.x 是一个重要的长期支持版本,于2019年发布。以下是其主要特性:
1. 语义化版本控制
Laravel 6.x 开始采用 语义化版本控制。这意味着:
- 主版本号 (6) 变化表示包含重大变更(可能不向后兼容)
- 次版本号 (如 6.1, 6.2) 表示新增功能(向后兼容)
- 修订号 (如 6.0.1, 6.0.2) 表示错误修复(向后兼容)
2. Job 中间件
允许在任务(Job)处理前后添加中间件逻辑,实现更精细的控制:
php
// 定义中间件
class LogJobExecution implements ShouldQueue
{
public function handle($job, $next)
{
Log::info('Job started: '.get_class($job));
$next($job);
Log::info('Job finished: '.get_class($job));
}
}
// 在任务中使用
class ProcessPodcast implements ShouldQueue
{
public function middleware()
{
return [new LogJobExecution];
}
}
3. 惰性集合
引入 LazyCollection 处理大型数据集时减少内存占用:
php
use Illuminate\Support\LazyCollection;
LazyCollection::make(function () {
$handle = fopen('large.log', 'r');
while (($line = fgets($handle)) !== false) {
yield $line;
}
})->chunk(1000)->each(function ($lines) {
// 分批处理
});
4. Eloquent 子查询增强
支持在查询构造器中直接使用子查询:
php
// 获取每个用户最新一条 Post 的标题
User::select([
'name',
Post::select('title')
->whereColumn('user_id', 'users.id')
->latest()
->limit(1)
])->get();
5. 门面文档注释
通过 _ide_helper.php 生成更准确的 IDE 自动补全提示:
php
// 示例自动生成注释
/**
* @method static \Illuminate\Database\Query\Builder where(...$params)
* @method static \Illuminate\Database\Query\Builder select(...$columns)
*/
class DB extends Facade {}
6. Blade 组件与指令
新增 @component 语法糖和自定义指令支持:
blade
<!-- 定义组件 -->
@component('alert', ['type' => 'danger'])
{{ $slot }}
@endcomponent
<!-- 自定义指令 -->
@directive('datetime', $var)
{{ $var->format('Y-m-d') }}
@enddirective
7. 宏可序列化
支持在 Macroable trait 中使用闭包序列化:
php
Collection::macro('toJson', function () {
return $this->map(fn ($item) => json_encode($item));
});
8. 密码重置优化
重构密码重置逻辑,支持多表认证(如用户/管理员):
php
// config/auth.php
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
'admins' => [
'provider' => 'admins',
'table' => 'admin_password_resets',
'expire' => 30,
],
]
9. 兼容性要求
- PHP 版本:>= 7.2.0
- 第三方包 :同步更新
laravel/ui(前端脚手架)、laravel/tinker等
升级建议
-
使用官方升级工具:
bashcomposer update php artisan ui:auth # 如需更新前端资源 -
检查废弃方法:替换
helpers.php中的array_和str_前缀函数 -
参考 官方升级指南
提示:Laravel 6 提供 2年 的错误修复支持和 1年 的安全更新支持。