通过类型提示,将服务提供者注入中间件

时间:2015-11-18 09:43:00

标签: php laravel dependency-injection

我试图在这样的中间件中注入服务提供者的实例:

class ApiMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  ApiManager $manager
     * @return mixed
     */
    public function handle($request, Closure $next, ApiManager $manager)
    {
    // more code goes here

每当我尝试访问导致API的任何路由时,我都会收到以下错误

Argument 3 passed to App\Http\Middleware\ApiMiddleware::handle() must be an instance of App\Http\Controllers\API\Helpers\ApiManager, none given

我绝对确定我已经正确注册了提供商,因为它适用于控制器。

路由中没有注入服务提供者实例的中间件的简单dd(ManagerInstance)给了我:

ApiManager {#176}

所以,它有效。

我的服务提供商看起来像这样:

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Http\Controllers\API\Helpers\ApiManager;

class ApiServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton('App\Http\Controllers\API\Helpers\ApiManager', function($app)
        {
            return new ApiManager($app['config']['manager']);
        });
    }
}

它已在'providers'数组中注册。

我的问题是:

  • 是否可以注入中间件?(我怀疑这是实际问题,而不是我已经做过的事情。)

  • 如果没有,那么这会有什么好办法吗?

    • 目前,我正在使用此类作为解决方法 - $manager = app()->make('App\Http\Controllers\API\Helpers\ApiManager');

当前的中间件:

<?php

namespace App\Http\Middleware;

use Closure;

class ApiMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $manager = app()->make('App\Http\Controllers\API\Helpers\ApiManager');

        return ! $manager->verifykey($request->get('key'))
            ? $manager->returnResponse(null, ['message' => KEY_INVALID, 'http_status' => 401], 401)
            : $next($request);
    }
}

我希望它看起来像:

class ApiMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next, ApiManager $manager)
    {
        return ! $manager->verifykey($request->get('key'))
            ? $manager->returnResponse(null, ['message' => KEY_INVALID, 'http_status' => 401], 401)
            : $next($request);
    }
}

1 个答案:

答案 0 :(得分:3)

像这样更新你的中间件:

class ApiMiddleware
{
    /**
     * The ApiManager implementation.
     *
     * @var ApiManager
     */
    protected $manager;

    /**
     * Create a new middleware instance.
     *
     * @param  ApiManager  $auth
     * @return void
     */
    public function __construct(ApiManager $manager)
    {
        $this->manager = $manager;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        return ! $this->manager->verifykey($request->get('key'))
            ? $this->manager->returnResponse(null, ['message' => KEY_INVALID, 'http_status' => 401], 401)
            : $next($request);
    }
}