PHP code example of aidan-casey / laravel-route-binding

1. Go to this page and download the library: Download aidan-casey/laravel-route-binding library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aidan-casey / laravel-route-binding example snippets


use AidanCasey\Laravel\RouteBinding\Binder;

// Binds route parameters to the construct of the referenced class.
$viewModel = Binder::make(IndexViewModel::class);

// Overrides the "user" parameter.
$viewModel = Binder::make(IndexViewModel::class, [
    'user' => 2,
]);

use AidanCasey\Laravel\RouteBinding\Binder;

// Calls the "execute" method on "MyTestClass."
Binder::call(MyTestClass::class, 'execute');

// Overrides the "user" parameter.
Binder::call(MyTestClass::class, 'execute', [
    'user' => 2,
]);

// Calls the "execute" method on the existing instance.

$instance = new MyTestClass;

Binder::call($instance, 'execute');

use AidanCasey\Laravel\RouteBinding\Binder;
use Illuminate\Support\ServiceProvider;

class SomeServiceProvider extends ServiceProvider
{
    public function register(){
        $this->app->beforeResolving(MyClass::class, function ($class, $parameters, $app) {
            if ($app->has($class)) {
                return;
            }
            
            $app->bind($class, fn ($container) => Binder::make($class, $parameters));
        });
    }
}