Title: Laravel Dependency Injection in Middleware
Tags: php;laravel;ioc-container;middleware
Question: 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-&gt;app-&gt;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\Authenticat75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0handle()
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.

Here is the accepted answer: 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-&gt;authClient = $authClient;
public function handle(Request $request, Closure $next)
// do what you want here through $this-&gt;authClient
Comment for this answer: OP can use IoC Container to resolve implementation from abstraction. App75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0make() is your friend in that case.
Comment for this answer: @Burimi it's pretty tricky, I think using construct functions is clear enough for this usage

Here is another answer: The accepted answer doesn't work anymore since Laravel 5.3.4.
See complain on GitHub and upgrade guide which provide an alternative.
For example, a snippet from what I implemented to solve the current problem:
<?php
namespace App\Http\Middleware;
use Closure;
use App\Models\Website;
use \Illuminate\Http\Request;
class CheckPropertyOfWebsite
protected $website_owner_id;
public function __construct(Request $request)
$this-&gt;website_owner_id = Websit75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0findOrFail($request-&gt;website)-&gt;first()-&gt;user_id;
public function handle($request, Closure $next)
if ($request-&gt;user()-&gt;id !== $this-&gt;website_owner_id) {
return response(null, 500);
}
return $next($request);

Here is another answer: You are not allowed to change the method signature here. Simply you may use something like this:
public function handle($request, Closure $next) {
// Get the bound object to this interface from Service Provider
$authClient = app('App\Services\Contracts\AuthClientInterface');
// Now you can use the $authClient
Also, you may use a __construct method to achieve that, check the answer given by - Francis.TM.

Here is another answer: Laravel's IoC only handles constructor method injection for all objects by default. The IoC will only inject dependencies into functions/methods handled by the router. The can be a closure used to handle a route, or more commonly, Controller methods used to handle routes.
The IoC does not do method dependency injection for any other object by default. You can call methods yourself through the IoC and have it resolve dependencies, but the framework only does this itself for route handlers. You can take a look at this question/answer for a little more information on using method dependency injection outside of a controller: can I use method dependency injection outside of a controller?.
Handling this by injecting your dependency through your constructor is the correct way to go, if you want to continue to use dependency injection.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 4, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:02/16/16', 'FavoriteCount': 3.0, 'PostTypeId': 1, 'Tags': '<php><laravel><ioc-container><middleware>', 'Title': 'Laravel Dependency Injection in Middleware', 'ViewCount': 13910, 'id': 35439234, 'user_AboutMe': '<p>I love coding.  I code for work, I code for a hobby, and I frequently fall asleep thinking about coding.  #SOreadytohelp</p>\n', 'user_AccountId': 1068081.0, 'user_DisplayName': 'ajon', 'user_Reputation': 7580.0, 'user_Views': 888.0, 'user_id': 1068058.0}}