DEV Community

Discussion on: Laravel: Bind an interface based on request parameters

Collapse
 
esirei profile image
Esirei • Edited

The reason you couldn't get the parameters off the request is because you are calling request helper in the service class, where the application has not finished bootstrapping.

You can use closures to provide the concrete implementation when registering a binding.

Try this out.

public function register()
{
    $this->app->bind('App\Thingable', function ($app) {
        return $app->make(request()->user_name === 'userA' ? 'App\DependencyA' : 'App\DependencyB');
    });
}
Enter fullscreen mode Exit fullscreen mode