我正在使用laravel5.2,我跟着https://laravel.com/docs/5.3/middleware,因为我在Middleware文件夹中创建了MyMiddleware.php 这是代码。
<?php
namespace App\Http\Middleware;
use Auth;
use Closure;
class MyMiddleware {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string $role
* @return mixed
*/
public function handle($request, Closure $next, $role) {
echo $role;exit;
return $next($request);
}
}
在kernel.php中:
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\MyMiddleware::class
]
];
protected $routeMiddleware = [
'absurd' => \App\Http\Middleware\MyMiddleware::class,
];
在routes.php中:
Route::any('manager/dashboard', 'UserController@mndashboard')->middleware('absurd:Admin');
但我仍然在MyMiddleware.php第18行中遇到Error:ErrorException:App \ Http \ Middleware \ MyMiddleware :: handle()缺少参数3
我尝试了一切,但没有工作。 帮助需要Plz。!
答案 0 :(得分:-1)
为您的handle
方法添加默认值:
public function handle($request, Closure $next, $role='default_vale') {
echo $role;exit;
return $next($request);
}
更新
首先从Web组中删除中间件。然后将其添加到您的路线组中,如下所示
Route::group(['middleware' => ['web', 'absurd:admin']], function(){
//Your Routes
}