lenderspender/substitute-class-binding

该包已被弃用,不再维护。未建议替代包。

为Laravel添加替代类绑定

2.0.0 2022-04-04 20:04 UTC

This package is auto-updated.

Last update: 2023-03-04 22:11:10 UTC


README

您可以使用此包在路由中使用时自动解析类。

LenderSpender\SubstituteClassBinding\Http\Middleware\SubstituteClassBindings 中间件添加到 \App\Kernel 中的 $middleware 数组。

LenderSpender\SubstituteClassBinding\Routing\UrlRoutable 接口添加到您希望解析的类。

<?php 

declare(strict_types=1);

use LenderSpender\SubstituteClassBinding\Routing\UrlRoutable;

class Foo implements UrlRoutable
{
    public $id = 1;
    
    public function __construct(array $properties)
    {
        $this->id = $properties['id'];   
    }

    public static function resolveRouteBinding($value)
    {
        return new Foo(['id' => $value]);
    }

    public function getRouteKey()
    {
        return $this->id;
    }

    public function getRouteKeyName() : string
    {
        return 'id';
    }
}

现在在路由中使用时,路由将自动解析。

Route::get('/foo/{foo}', function (Foo $foo) {
    echo $foo->id; // 12345 when calling /foo/12345
});