Happy New Year!
编程

Laravel 10 正式发布

1158 2023-02-15 00:02:09

Laravel 10 现已发布,包括PHP v8.1最低版本要求、新的Laravel Pennant包、可调用的验证规则、原生类型声明等等

Laravel 发布日程表

Laravel 9 之前,主框架版本每年发布两次,即每6个月发布一次。从 Laravel 9 开始,核心团队按照年度计划,在 2022 年 2 月发布 Laravel 9(原计划于 2021 年 9 月 发布):

Laravel uses a variety of community-driven packages as well as nine Symfony components for a number of features within the framework. Symfony 6.0 is due for release in November. For that reason, we are choosing to delay the Laravel 9.0 release until 2022.

By delaying the release, we can upgrade our underlying Symfony components to Symfony 6.0 without being forced to wait until September 2022 to perform this upgrade. In addition, this better positions us for future releases as our yearly releases will always take place two months after Symfony's releases.

此后每年一次的主版本发布计划:

  • Laravel 9:  2022 年 2 月 8 日
  • Laravel 10:  2023 年 2 月 7 日
  • Laravel 11: 2024 年 第一季度

Laravel 9 将持续接收 bug 修复直到 2023年8月8日,接收安全修复直到 2024年2月14日

Laravel 10 的 bug 修复将持续到 2024年8月6日,安全修复将持续到2025年2月14日。

接下来我们一起来看看Laravel 10 的一些新特性:

Laravel 10 丢弃对PHP 8.0的支持 

在 Laravel 10,Laravel 框架将丢弃对 PHP <=v8.0 的支持。PHP 版本的最低支持将是  ^8.1。查看 master 分支和 9.x 的不同,我们可以看到框架中使用的8.1特性,比如只读属性。

Laravel Pennant

Laravel Pennant是由Laravel团队开发的一个包,它将与Laravel 10一起提供,并为您的应用程序提供功能标志(Feature Flag)。

Feature flags 使您能够自信地逐步推出新的应用程序功能,A/B测试新的界面设计,补充基于主干的开发策略,等等。

该软件包是核心团队提供的官方软件包阵容中的最新软件包,这意味着我们现在拥有一个构建良好、测试良好的软件包,为我们提供了一些很棒的功能。

Laravel 处理层(Process Layer)

Laravel Process 服务使测试和运行CLI进程成为可能。

use Illuminate\Support\Facades\Process;
 
$result = Process::run('ls -la');
 
$result->successful();
$result->failed();
$result->exitCode();
$result->output();
$result->errorOutput();
$result->throw();
$result->throwIf($condition);

处理层包含了一些开箱即用的新特性,比如:

  • 流畅的处理方法,用以在运行前创建处理实例
  • 收到输出时的处理
  • 异步处理
  • 处理工具
  • 通过 fake() 方法提供丰富的测试功能
  • 防止测试过程中的杂散过程

测试处理从未如此容易

Laravel 10 框架中的原生类型声明

Laravel 10 中,应用框架代码将使用原生类型声明。这意味着由框架生成的用户空间代码都将有类型提示及返回类型。我们的文章讨论了该方法的注意事项,我们认为在将来创建新项目时,您会喜欢添加的类型。

类型的添加方式为Laravel项目带来了最新的PHP类型提示功能,而不会破坏框架级别的向后兼容性:

  • 返回类型
  • 方法参数
  • 在可能的情况下删除冗余注释R
  • 允许闭包参数中使用用户空间类型
  • 不包含类型属性

Invokable 验证规则是默认规则

从 Laravel 10 起,验证规则默认是 invokable 的。如果你通过 artisan 创建新的验证规则:

# Laravel 9 creates a rule class that implements the
# Illuminate\Contracts\Validation\Rule interface
artisan make:rule Uppercase
 
# Laravel 9 flag to create an invokable and implicit rule
artisan make:rule Uppercase --invokable
artisan make:rule Uppercase --invokable --implicit
 
# Laravel 10 creates an invokable rule by default
artisan make:rule Uppercase
 
# Laravel 10 implicit rule
artisan make:rule Uppercase --implicit

Profile option for tests

Laravel 10 的另一个新特性是 --profile 选项,使你更容易在应用中找到慢测试。

--profile 选项用来帮你保证测试速度,修复缓慢的测试,或更好地对它们进行分组,使其更容易不一直运行

新增 String Password helper

Str::password 方法可以生成一个给定长度的安全随机密码。密码由字母、数字、符号和空格组成。默认长度为32个字符:

use Illuminate\Support\Str;
 
$password = Str::password();
 
// 'EbJo2vE-AS:U,$%_gkrV4n,q~1xy/-_4'
 
$password = Str::password(12);
 
// 'qwuar>#V|i]N'

Laravel 9 中弃用的功能

Laravel 9 中标记为弃用的方法,在 Laravel 10 中将被移除。升级向导中可以看到所有弃用的方法和对潜在影响的评估,以及如何升级到最新版本。

以下是撰写本文时 Laravel 框架 master 分支 和 9.x 分支中发现的一些不同:

更多…