lenderspender/substitute-class-binding

This package is abandoned and no longer maintained. No replacement package was suggested.

Add substitute class bindings to 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

You can use this package to automatically resolve classes when used in routes.

Add the LenderSpender\SubstituteClassBinding\Http\Middleware\SubstituteClassBindings middleware to your $middleware array in \App\Kernel.

Add the LenderSpender\SubstituteClassBinding\Routing\UrlRoutable interface to the class you wish to be resolved.

<?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';
    }
}

Now the route will automatically be resolved when using it in the route.

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