13

Bindings

I'm using bindings in my service provider between interface and implementation:

public function register()
{
    $this->app->bind('MyInterface', MyImplementation::class);
}

Middleware

In my middleware, I add an attribute to the request:

public function handle($request, Closure $next)
{
    $request->attributes->add(['foo' => 'bar]);
    return $next($request);
}

Now, I want to access foo in my service provider

public function register()
{
    $this->app->bind('MyInterface', new MyImplementation($this->request->attributes->get('foo')); // Request is not available
}

The register() is called before applying the middleware. I know.

I'm looking for a technique to 'rebind' if the request->attributes->get('foo') is set

4
  • 1
    But isn't this a bad practice in general? Launching SPs is a part of bootstrap process, handling a request happens later. It sounds somewhat awkward - changing the boot process based on request. Shouldn't you solve your problem some other way? Commented May 31, 2016 at 23:35
  • Yes, you're totally right. But, now, I've to instantiate concrete implementations, which isn't great maintainable. Commented Jun 1, 2016 at 3:20
  • Why? You could bind another implementation to the interface in your Middleware, based on $request parameters Commented Jun 1, 2016 at 4:52
  • No, no, Denis ;-) The only place I bind is in the Service Provider. But in my case, I need an attribute (added in the middleware) from the request object for my binding (see my register method in my main question) Commented Jun 1, 2016 at 5:41

4 Answers 4

28

Try like this:

public function register()
{
    $this->app->bind('MyInterface', function () {
        $request = app(\Illuminate\Http\Request::class);

        return app(MyImplementation::class, [$request->foo]);
    }
}

Binding elements works like this that they will be triggered only when they are call.

5
  • Thanks! You mean defered? Commented Jun 1, 2016 at 4:51
  • Exactly I mean defered. Commented Jun 1, 2016 at 4:53
  • Yes, it did. Thanks. But is this a bad practice? Commented Jun 1, 2016 at 6:25
  • 4
    Absolutely not. This is how IoC container works and it's it one of advantage. Commented Jun 1, 2016 at 6:28
  • For those who might wonder app refers to the Laravel helper function: laravel.com/docs/8.x/helpers#method-app
    – RazorHead
    Commented Oct 5, 2021 at 20:09
12

In service provider You can also access Request Object by:

public function register()
{
    $request = $this->app->request;
}
1

The accepted answer is good, however it does not address the issues regarding DI. So in your Service Provider you need:

public function register()
{
    $this->app->bind('MyInterface', function () {
        return new MyImplementation(request()->foo);
    }
}

But you need to be careful with DI. If you do this in your Controller:

class MyController extends Controller
{
    public function __construct(MyInterface $myInterface)
    {
        $this->myInterface = $myInterface;
    }
}

It will NOT work! The constructor of the controller is called BEFORE the group middleware is applied, so the foo parameter will be null on MyImplementation.

If you want to use DI, you need to either resolve it using App::make(MyInterface::class) outside of the constructor, or even better pass your dependency in the Controller's method:

class MyController extends Controller
{
    public function index(MyInterface $myInterface)
    {
        $myInterface->getFoo();
    }
}

Above will work because the controller's method is executed after the middlewares are applied.

This is the flow of a laravel request:

  1. Global middleware run
  2. Target controller's __construct run
  3. Group middleware run
  4. Target controller's method/action run (in above case index)
0

Try this

public function register()
{
    $this->app->bind('MyInterface', function ($app) {
        return new MyImplementation(request()->foo);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.