流明:jwt-auth不存在方法句柄,完成了中间件

时间:2016-05-25 22:47:52

标签: php laravel jwt lumen

嘿所以我刚刚更新到Lumen 5.2并遇到了jwt-auth的问题。我已按照所有说明更新了我的app.php文件,包括所有中间件。我也作曲家需要照亮/路由和照亮/认证。 但是我得到了错误:

BadMethodCallException in Macroable.php line 81: Method handle does not exist. in Macroable.php line 81 at ResponseFactory->__call('handle', array(object(Request), object(Closure)))

我似乎无法理解这个错误?

这是我的boostrap / app.php供参考:

<?php

require_once __DIR__.'/../vendor/autoload.php';

try {
    (new Dotenv\Dotenv(__DIR__.'/../'))->load();
} catch (Dotenv\Exception\InvalidPathException $e) {
    //
}

/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| Here we will load the environment and create the application instance
| that serves as the central piece of this framework. We'll use this
| application as an "IoC" container and router for this framework.
|
*/

//$app = new Laravel\Lumen\Application(
//    realpath(__DIR__.'/../')
//);

// For nested route groups to work
$app = new Fremail\NestedRouteGroups\Application(
    realpath(__DIR__.'/../')
);

 $app->withFacades();

$app->configure('jwt');
$app->configure('auth');

class_alias(Tymon\JWTAuth\Facades\JWTAuth::class, 'JWTAuth');
class_alias(Tymon\JWTAuth\Facades\JWTFactory::class, 'JWTFactory');


 $app->withEloquent();

/*
|--------------------------------------------------------------------------
| Register Container Bindings
|--------------------------------------------------------------------------
|
| Now we will register a few bindings in the service container. We will
| register the exception handler and the console kernel. You may add
| your own bindings here if you like or you can make another file.
|
*/

$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\Handler::class
);

$app->singleton(
    Illuminate\Contracts\Console\Kernel::class,
    App\Console\Kernel::class
);

$app->singleton(
    Illuminate\Cache\CacheManager::class,
    function ($app) {
        return $app->make('cache');
    }
);

$app->singleton(
    Illuminate\Auth\AuthManager::class,
    function ($app) {
        return $app->make('auth');
    }
);

$app->singleton(
    Illuminate\Contracts\Routing\ResponseFactory::class,
    Illuminate\Routing\ResponseFactory::class
);


/*
|--------------------------------------------------------------------------
| Register Middleware
|--------------------------------------------------------------------------
|
| Next, we will register the middleware with the application. These can
| be global middleware that run before and after each request into a
| route or middleware that'll be assigned to some specific routes.
|
*/

$app->middleware([
    Illuminate\Contracts\Routing\ResponseFactory::class,
//     // Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
//     // Illuminate\Session\Middleware\StartSession::class,
//     // Illuminate\View\Middleware\ShareErrorsFromSession::class,
//    Laravel\Lumen\Http\Middleware\VerifyCsrfToken::class,
 ]);

// Middleware for authentication for the API
$app->routeMiddleware([
    'auth'        => App\Http\Middleware\Authenticate::class,
    'jwt.auth'    => Tymon\JWTAuth\Middleware\GetUserFromToken::class,
    'jwt.refresh' => Tymon\JWTAuth\Middleware\RefreshToken::class,
]);

/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
*/

$app->register(App\Providers\GuardServiceProvider::class);
$app->register(Tymon\JWTAuth\Providers\JWTAuthServiceProvider::class);

/*
|--------------------------------------------------------------------------
| Load The Application Routes
|--------------------------------------------------------------------------
|
| Next we will include the routes file so that they can all be added to
| the application. This will provide all of the URLs the application
| can respond to, as well as the controllers that may handle them.
|
*/

$app->group(['namespace' => 'App\Http\Controllers'], function ($app) {
    require __DIR__.'/../app/Http/routes.php';
});

return $app;

感谢您的帮助

1 个答案:

答案 0 :(得分:4)

我的回答一定要迟到,但对于未来的googlers,我的解决方法就是这样(Lumen 5.3):

我在bootstrap / app.php中设置了auth中间件:

$app->routeMiddleware([
    'auth' => App\Http\Middleware\Authenticate::class
]);