流明角色中间件缺少参数

时间:2015-12-14 15:21:37

标签: php laravel routing middleware lumen

我是Lumen的新手,我正试图从Lumen文档中的一个例子中获取角色中间件: http://lumen.laravel.com/docs/middleware#middleware-parameters

但是,只要我将中间件添加到路由中,就像这样:

wxMKdir

我收到错误:

$app->get('/', ['middleware' => 'role:editor', 'uses' => 'IndexController@index']);

当我删除中间件时,一切都按预期工作

at Application->Laravel\Lumen\{closure}('2', 'Missing argument 3 for App\Http\Middleware\RoleMiddleware::handle()', '/Users/peter/app/Http/Middleware/RoleMiddleware.php', '17', array('request' => object(Request), 'next' => object(Closure))) in RoleMiddleware.php line 17

在app.php中:

$app->get('/', ['uses' => 'IndexController@index']);

应用\ HTTP \中间件\ RoleMiddleware.php:

$app->middleware([
    Illuminate\Cookie\Middleware\EncryptCookies::class,
    Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
    Illuminate\Session\Middleware\StartSession::class,
    Illuminate\View\Middleware\ShareErrorsFromSession::class,
    Laravel\Lumen\Http\Middleware\VerifyCsrfToken::class,
    App\Http\Middleware\RoleMiddleware::class
]);

希望有人可以提供帮助

2 个答案:

答案 0 :(得分:2)

似乎您必须将中间件注册为RouteMiddleware,而不是:

$app->middleware([
    Illuminate\Cookie\Middleware\EncryptCookies::class,
    Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
    Illuminate\Session\Middleware\StartSession::class,
    Illuminate\View\Middleware\ShareErrorsFromSession::class,
    Laravel\Lumen\Http\Middleware\VerifyCsrfToken::class,
    App\Http\Middleware\RoleMiddleware::class
]);

必须是:

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

$app->routeMiddleware([
    "role" => App\Http\Middleware\RoleMiddleware::class
]);

然后它运作正常!

答案 1 :(得分:0)

根据错误,我了解到您没有在handle()中提供参数,即第3个参数。

您的handle()应该是这样的。

public function handle($request, Closure $next, $role)

第三个参数$role根据您的代码

存储值editor