Lumen配置Jwt-auth时的BUG处理

前言

  玩Lumen真的会遇到很多坑,jwt-auth就是其中之一,首先其对Lumen的配置教程比较少,其次可能是本人对Laravel和Lumen还不是太熟悉导致。

  成功配置好Lumen+jwt-auth的时候可能还会出现问题:

1
Target [Illuminate\\Contracts\\Routing\\ResponseFactory] is not instantiable while building [Tymon\\JWTAuth\\Middleware\\GetUserFromToken]

解决办法

  1. bootstrap/app.php中添加如下代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    $app->singleton(
    Illuminate\Auth\AuthManager::class,
    function ($app) {
    return $app->make('auth');
    }
    );
    $app->singleton(
    Illuminate\Cache\CacheManager::class,
    function ($app) {
    return $app->make('cache');
    }
    );
    $app->register(App\Providers\AppServiceProvider::class);
  2. 修改app/Providers/AppServiceProvider.php,在register方法中添加如下代码:

    1
    2
    3
    4
    $this->app->singleton('Illuminate\Contracts\Routing\ResponseFactory', function ($app)
    {
    return new ResponseFactory($app['Illuminate\Contracts\View\Factory'], $app['Illuminate\Routing\Redirector']);
    });
  3. 大功告成了,问题已经解决,可以愉快地开启你的Lumen+jwt之旅!

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    $app->singleton(
    Illuminate\Auth\AuthManager::class,
    function ($app) {
    return $app->make('auth');
    }
    );
    $app->singleton(
    Illuminate\Cache\CacheManager::class,
    function ($app) {
    return $app->make('cache');
    }
    );
    $app->register(App\Providers\AppServiceProvider::class);

------ 本文结束 ------

版权声明

yoGa's Blog by yoga lee is licensed under a Creative Commons BY-NC-ND 4.0 International License.
yoga lee创作并维护的yoGa's Blog采用创作共用保留署名-非商业-禁止演绎4.0国际许可证
本文首发于yoGa's Bloghttp://yoga.ibye.cn ),版权所有,若需转载请注明出处,谢谢支持。