Lumen:jwt-auth 不存在方法句柄,已完成中间件

Posted

技术标签:

【中文标题】Lumen:jwt-auth 不存在方法句柄,已完成中间件【英文标题】:Lumen: Method handle does not exist with jwt-auth, done the middleware 【发布时间】:2016-09-23 18:01:51 【问题描述】:

嘿,所以我刚刚更新到 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;

感谢您的帮助

【问题讨论】:

在 Laravel 中,抛出该异常的方法试图动态处理对类的调用......你能确认它在 Lumen 中是一样的吗?似乎有东西在一个类上调用handle(),但该类中不存在该方法。 是的,我认为这基本上也是这里发生的事情。有些东西试图在 ResponseFactory 上调用“句柄”,但它不存在。 imgur.com/SIKYA52 你能显示导致错误的违规代码吗? @Feek 最终只是切换到了 Laravel。光秃秃的错误就消失了欢呼声 ?????????? 【参考方案1】:

我的回答肯定晚了,但对于未来的谷歌人来说,这是我解决它的方法(Lumen 5.3):

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

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

【讨论】:

拯救了我的一天!非常感谢!【参考方案2】:

我也遇到了和你一样的错误。

但是我在路由文件上犯了一个错误。 我写路由文件:

 $app->middleware(['auth'],function()use($app)
    $app->group(['prefix'=>'pur'],function()use($app)
        $app->get('list','PurchaseController@purList');
    );
);

这是错误的。 先定义路由:

$app->group(['middleware' => ['auth']], function () use ($app) 
$app->group(['prefix'=>'pur'],function()use($app)
    $app->get('list','PurchaseController@purList');
);

);

我的错误信息:

【讨论】:

以上是关于Lumen:jwt-auth 不存在方法句柄,已完成中间件的主要内容,如果未能解决你的问题,请参考以下文章

如何使用相同的身份验证保护验证多种类型的用户 [Lumen]

在 jwt-auth laravel 中获取自定义声明

方法 Illuminate\Auth\RequestGuard::attempt 不存在

Lumen 5.6 - php artisan db:seed 出现错误“类 DatabaseSeeder 不存在”

在流明 jwt 令牌中设置过期时间

(API) Laravel 7 上不允许使用 tymon/jwt-auth 的 405 方法