中间件中的 Laravel 依赖注入
2022-08-30 21:39:23
我使用的是Laravel-5.0的默认中间件,但我更改了句柄函数的签名,使其具有:Authentication
public function handle($request, Closure $next, AuthClientInterface $authClient)
我还在服务提供商处注册了:AuthClientInterface
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')
);
});
}
但是,尽管如此,我还是看到以下错误:
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...
有人能看到我做错了什么吗?
编辑:我确实通过将HelioAuthClientInterface传递到中间件的构造器中来使其工作。但是,我认为除了构造函数之外,IoC容器还会将依赖项注入到方法中。