在Laravel5.2中,将路径中的参数传递给MyMiddleware,但得到Missing参数3

时间:2016-09-10 09:34:41

标签: laravel-5.2

我正在使用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。!

1 个答案:

答案 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
}