编程

Laravel 8.77 发布

1355 2021-12-23 02:36:07

Laravel 发布了版本8.77,此次更新包括以下一些特性:改进了属性造型cast和访问器accessor,请求的date()方法以DateTime实例获取数据中的日期,MAC地址验证,在storage disks中设置自定义临时URLs。

属性转换器/获取器的改进

Taylor Otwell 贡献了一个定义属性访问器accessors和修改器mutators 的新方式:

// 旧方式
public function setTitleAttribute($value)
{
    $this->attributes['title'] = strtolower($value);
}
 
// 新方式
protected function title(): Attribute
{
    return new Attribute(
        set: fn ($value) => strtolower($value),
    );
}

以下例子包含get和set实现:

/**
 * Get the user's title.
 */
protected function title(): Attribute
{
    return new Attribute(
        get: fn ($value) => strtoupper($value),
        set: fn ($value) => strtolower($value),
    );
}

请求Request实例中添加了DateTime解析

@Italo 贡献了date()方法,用以将request中的数据转换成日期实例

// 以前
if ($date = $request->input('when')) {
    $date = Carbon::parse($datetime);
}
 
// 以后
$date = $request->date('when');

Per-connection Predis Prefixes

Predis 前缀

Ben Tidy 贡献了让每个Predis连接都能使用前缀的功能,配置示例:

'redis' => [
    'client' => env('REDIS_CLIENT', 'predis'),
 
    'default' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => env('REDIS_DB', 0),
        'prefix' => env('REDIS_PREFIX', 'prefix:'),
    ],
 
    'cache' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => env('REDIS_CACHE_DB', 1),
        'prefix' => env('REDIS_PREFIX', 'prefix2:'),
    ],
],

MAC 地址验证规则

Bilal AI-Massry 贡献了 mac_address 验证规则,用以验证MAC地址

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['mac' => 'foo'], ['mac' => 'mac_address']);
$this->assertFalse($v->passes());
 
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['mac' => '01-23-45-67-89-ab'], ['mac' => 'mac_address']);
$this->assertTrue($v->passes());
 
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['mac' => '01-23-45-67-89-AB'], ['mac' => 'mac_address']);
$this->assertTrue($v->passes());

为Storage定义临时URL方法

Ash Allen 贡献了为Storage Facade设置自定义临时URL的逻辑. eg.:

Storage::disk('local')
    ->buildTemporaryUrlUsing(function ($path, $expiration, $options) {
      return 'using local';
    });
 
// $url is: 'using local'
$url = Storage::temporaryUrl('file.jpg', now()->addMinutes(5));

 

Changelog:

v8.77.0

Added

  • Attribute Cast / Accessor Improvements (#40022)
  • Added Illuminate/View/Factory::renderUnless() (#40077)
  • Added datetime parsing to Request instance (#39945)
  • Make it possible to use prefixes on Predis per Connection (#40083)
  • Added rule to validate MAC address (#40098)
  • Added ability to define temporary URL macro for storage (#40100)

Fixed

  • Fixed possible out of memory error when deleting values by reference key from cache in Redis driver (#40039)
  • Added Illuminate/Filesystem/FilesystemManager::setApplication() (#40058)
  • Fixed arg passing in doesntContain (739d847)
  • Translate Enum rule message (#40089)
  • Fixed date validation (#40088)
  • Don’t allow models and except together in PruneCommand.php (f62fe66)

Changed

  • Passthru Eloquent\Query::explain function to Query\Builder:explain for the ability to use database-specific explain commands (#40075)