Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Dependency Injection in Middleware

I am using Laravel-5.0's default Authentication Middleware, but I changed the signature of the handle function to have:

public function handle($request, Closure $next, AuthClientInterface $authClient)

I also registered AuthClientInterface in a Service Provider with:

public function register()
{
    $this->app->bind('App\Services\Contracts\AuthClientInterface', function()
    {
        return new AuthClient(
            env('AUTH_SERVER_URL'),
            env('AUTH_SESSION_URL'),
            env('AUTH_CLIENT_ID')
        );
    });
}

However, despite this, I am see the following error:

Argument 3 passed to HelioQuote\Http\Middleware\Authenticate::handle() 
must be an instance of 
HelioQuote\Services\Contracts\HelioAuthClientInterface, none given, 
called in C:\MyApp\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php on line 125 and defined...

Can anyone see what I am doing wrong?

EDIT: I did get it working by passing the HelioAuthClientInterface into the constructor of the middleware. However I thought the IoC container would also inject the dependency to methods in addition to the constructor.

like image 338
ajon Avatar asked Feb 16 '16 17:02

ajon


People also ask

Does Laravel use dependency injection?

The Laravel service container is a powerful tool for managing class dependencies and performing dependency injection. Dependency injection is a fancy phrase that essentially means this: class dependencies are "injected" into the class via the constructor or, in some cases, "setter" methods.

What is Laravel dependency injection?

In Laravel, dependency injection is the process of injecting class dependencies into a class through a constructor or setter method. This allows your code to look clean and run faster. Dependency injection involves the use of a Laravel service container, a container that is used to manage class dependencies.

Is Laravel a middleware?

Laravel Middleware provides a convenient mechanism for assessing and sifting through HTTP demands that enter your application. All of these middleware are found within the app/Http/Middleware registry. Laravel Middleware acts as a bridge between a request and a reaction.

What's a main purpose of middleware in Laravel?

Middleware provide a convenient mechanism for inspecting and filtering HTTP requests entering your application. For example, Laravel includes a middleware that verifies the user of your application is authenticated.


1 Answers

You cannot do dependency injection at handle method in a Request directly, do that in a constructor.

Middleware is invoked by call_user_func, so any injection here will not be work.

<?php

namespace App\Http\Middleware;

use Closure;
use App\Foo\Bar\AuthClientInterface; # Change this package name

class FooMiddleware
{
  protected $authClient;

  public function __construct(AuthClientInterface $authClient)
  {
    $this->authClient = $authClient;
  }

  public function handle(Request $request, Closure $next)
  {
    // do what you want here through $this->authClient
  }
}
like image 178
Francis.TM Avatar answered Oct 02 '22 17:10

Francis.TM