编程

Laravel 9.38 发布

651 2022-11-02 16:53:08

Laravel 团队本周发布了 9.38 。这些更新包括单例 artisan 命令,条件性设置 notification 通知中间件,queueable 通知可配置最大异常数,等等。

Today's Laravel 9.38.0 release introduces Isolatable commands. Ensure that only one instance of an Artisan command is running at a time. ⌛

A cinch to use - and we've added support for this to the "migrate" command already. Handy during deployment!

Docs: https://t.co/35lcZJZHPO pic.twitter.com/tLln5thJHO

— Taylor otwell ? (@taylorotwell) November 1, 2022

单例 Artisan 命令

Oliver Nybroe 贡献了 Isolatable 接口,让你的命令一次只运行一个进程变得非常容易。 

namespace App\Console\Commands;
 
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\Isolatable;
 
class SendEmails extends Command implements Isolatable
{
    // ...
}-

引入 Isolatatable 接口的首个 artisan 命令是 artisan migrate 命令。使用 --isolated 标志你可以限制将 migrate 限制在一个激活的进程中,确保不会出现两个服务同时运行 migrate 命令这样的情况:

php artisan migrate --isolated

注意:migrate 中的 isolated 不是默认的情况,需要使用 --isolated 以减少产生破坏性改变的可能。

更多详情可查看 Isolatable 命令相关文档

设置 session 保存的 handler

Samuel Štancl 贡献了 setHandler 方法给 session Store 类。PR 描述说明了这一方法的用处:

The use case is that when you're changing database connections on the fly, the database session driver can run into issues where it tries to run queries on a DB connection that no longer exists...By adding the setter, I can reconstruct the DatabaseSessionHandler on the fly and make it use the correct DB connection.

Slug 助手词典

贡献者@Dhemy :在调用 Str::slug() 时,可为指定字符自定义字典:

$ php artisan tinker
 
>>> Str::slug('500$ bill');
=> "500-bill"
 
>>> Str::slug(
...     title: '500$ bill',
...     dictionary: ['@' => 'at', '$' => 'dollar']
... );
=> "500-dollar-bill"

条件性设置通知中间件 

Andrew Monty 贡献了基于notifiable 和 channel 实例对通知中间件进行设置的能力。PR 中的示例:

public function middleware($notifiable, $channel)
{
    if ($notifiable instance of User && $notifiable->isAdmin()) {
        return [];
    }
 
    if ($channel == 'email') {
        return [new RateLimited('mailgun')];
    }
 
    return [];
}

添加 touchQuietly 模型快捷方法

Craig Anderson 贡献了 touchQuietly() 快捷方法,用以在不触碰事件的情况下可以接触到 update 时间戳:

$model->touchQuietly();

从 group 中移除中间件

贡献者Mateus Guimarães 。该特性可用于动态注册或移除中间件。

$router->removeMiddlewareFromGroup(
    'web',
    'test-middleware'
);

Queueable notifications 可以设置最大 exceptions 数

Andrew Monty contributed the ability for queued notifications to use max exceptions. Setting max exceptions is helpful in a setting where you have many retries. Perhaps the queued notification is rate limited by a third party or otherwise not going to succeed, and you want to customize the max number of allowed exceptions.

See Pull Request #44773 for more details and examples.

Release Notes

You can see the complete list of new features and updates below and the diff between 9.37.0 and 9.38.0 on GitHub. The following release notes are directly from the changelog:

v9.38.0

Added

  • Added Illuminate/Routing/Route::flushController() (#44393)
  • Added Illuminate/Session/Store::setHandler() (#44736)
  • Added dictionary to slug helper (#44730)
  • Added ability to set middleware based on notifiable instance and channel (#44767)
  • Added touchQuietly convenience method to Model (#44722)
  • Added Illuminate/Routing/Router::removeMiddlewareFromGroup() (#44780)
  • Allow queueable notifications to set maxExceptions (#44773)
  • Make migrate command isolated (#44743, ac3252a)

Fixed

  • Fixed whenPivotLoaded(As) api resource methods when using Eloquent strict mode (#44792)
  • Fixed components view error when using $attributes in parent view (#44778)
  • Fixed problem with disregarding global scopes when using existOr and doesntExistOr methods on model query (#44795)

Changed

  • Recompiles views when necessary (#44737)
  • Throw meaningful exception when broadcast connection not configured (#44745)
  • Prevents booting of providers when running env:encrypt (#44758)
  • Added nonce for preloaded assets (#44747)
  • Inherit crossorigin attributes while preloading view (#44800)