lenderspender / substitute-class-binding
Add substitute class bindings to Laravel
Installs: 2 691
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 5
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- illuminate/contracts: ^9.0
Requires (Dev)
- lenderspender/php-cs-fixer-rules: dev-master
- orchestra/testbench: ^7.0
- phpstan/phpstan: ^1.3.2
- phpunit/phpunit: ^9.5
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 });