Laravel 8.x十大核心特性深度解析

好的,Laravel 8.x 版本引入了多项重要特性与改进:

1. Laravel Jetstream

  • 提供了一套现代化的应用脚手架,整合了 LivewireInertia.js 栈。
  • 包含登录、注册、邮箱验证、双因素认证、会话管理、API 支持等开箱即用的功能。
  • 替代了之前的 Laravel UI,提供更丰富的起点。

2. 模型工厂类 (Model Factory Classes)

  • 模型工厂从基于函数的定义转变为基于类的定义。
  • 例如:
php 复制代码
// Laravel 7.x 及之前 (database/factories/UserFactory.php)
$factory->define(User::class, function (Faker $faker) {
    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
    ];
});

// Laravel 8.x (database/factories/UserFactory.php)
class UserFactory extends Factory {
    public function definition() {
        return [
            'name' => $this->faker->name(),
            'email' => $this->faker->unique()->safeEmail(),
        ];
    }
}

更符合现代 PHP 的面向对象风格。

3. 迁移压缩 (Migration Squashing)

  • 允许将应用程序的多个迁移文件压缩成单个 SQL 文件。
  • 使用命令 php artisan schema:dump
  • 可以显著减少运行测试时加载迁移的数量,提升测试速度。

4. 改进的路由缓存 (Improved Route Caching)

  • 使用 php artisan route:cache 时,现在会先执行 php artisan route:clear,避免旧缓存问题。
  • 提高了路由缓存的可靠性。

5. 模型命名空间调整 (默认 App\Models)

  • artisan make:model 命令现在默认将模型创建在 App\Models 目录下。
  • 更符合现代应用程序的目录组织结构。之前的版本默认在 App 根目录下。

6. 队列批处理 (Job Batching)

  • 允许将一组作业作为一个可管理的"批处理"进行调度和监控。
  • 提供方法跟踪批处理的进度、状态和完成情况。
php 复制代码
use Illuminate\Bus\Batch;
use Illuminate\Support\Facades\Bus;
use App\Jobs\ProcessPodcast;
use Throwable;

$batch = Bus::batch([
    new ProcessPodcast(1),
    new ProcessPodcast(2),
    new ProcessPodcast(3),
])->then(function (Batch $batch) {
    // 所有作业成功完成...
})->catch(function (Batch $batch, Throwable $e) {
    // 批处理中首次检测到失败的作业...
})->finally(function (Batch $batch) {
    // 批处理执行完毕(无论成功或失败)...
})->dispatch();

7. 速率限制改进 (Rate Limiting Improvements)

  • 在路由中定义速率限制器变得更加灵活和强大。
  • 支持自定义响应和条件限制。
php 复制代码
Route::get('/api/users', function () {
    //
})->middleware(['throttle:api']);

可以在 App\Providers\RouteServiceProvider 中配置 api 限制器。

8. 时间测试助手改进 (Improved Time Testing Helpers)

  • Illuminate\Support\Carbon 新增了 setTestNow($now = null) 方法。
  • 提供了更简洁的方式来在测试中操纵时间。

9. 动态 Blade 组件属性

  • 允许将 HTML 属性动态传递给 Blade 组件。
blade 复制代码
<x-alert :$type :$message class="mt-4"/>

组件内部可以通过 {``{ $attributes }} 接收所有属性。

10. dispatchAfterResponse 方法

  • 允许在 HTTP 响应发送到浏览器之后才将作业推送到队列。
php 复制代码
ProcessPodcast::dispatchAfterResponse();

11. Eloquent 改进

  • whereHas / orWhereHas 嵌套支持:允许更复杂的关联查询。
  • doesntHave / orDoesntHave 嵌套支持
  • upsert 方法:实现"插入或更新"操作。
php 复制代码
Flight::upsert([
    ['departure' => 'LAX', 'destination' => 'NYC', 'price' => 150],
    ['departure' => 'LHR', 'destination' => 'AMS', 'price' => 200]
], ['departure', 'destination'], ['price']);

12. 测试改进

  • HTTP 测试断言 :新增了如 assertCreatedassertForbiddenassertNoContent 等断言方法。
  • 数据库测试 :新增了 expectsDatabaseQueryCount 方法,用于断言测试期间执行的数据库查询数量。

13. 事件监听器优化

  • 可以在事件监听器中返回 false 来停止事件的传播。

这些特性显著提升了 Laravel 的开发效率、代码组织、测试便利性和应用性能。

相关推荐
zx28596340017 小时前
Laravel 8.x 核心特性全面解析
php·laravel
Gh0st_Lx19 小时前
【6】为什么有了 HTTP/1.1 ,还要 HTTP/2 和 HTTP/3
网络协议·http·php
xingpanvip19 小时前
星盘接口开发文档:组合三限盘接口指南
android·开发语言·前端·python·php·lua
灰子学技术21 小时前
Envoy TCP 层面的 Metric 指标分析
开发语言·网络·网络协议·tcp/ip·php
Johnstons21 小时前
TCP Reset(RST)异常是什么?一文讲透连接被动中断的识别方法、适用场景、与超时断开的边界及排查清单
网络协议·tcp/ip·php·es·抓包分析
REDcker1 天前
Linux信号机制详解 POSIX语义与内核要点 sigaction与备用栈实践
linux·运维·php
REDcker1 天前
浏览器端Web程序性能分析与优化实战 DevTools指标与工程清单
开发语言·前端·javascript·vue·ecmascript·php·js
云云只是个程序马喽1 天前
AI漫剧创作系统开发定制指南
人工智能·小程序·php
xxjj998a2 天前
Laravel4.x核心特性全解析
android·mysql·laravel
niucloud-admin2 天前
PHP V6 单商户常见问题——云编译报错处理
php