我这几天面临一个问题,当我试图从我的路由器参数传递给我的中间件,以检查身份验证的用户有权限访问路线。 我如何可以传递路由到中间件的说法?
Answer 1:
我试图做它和它的作品对我非常好:在我的路线文件:
Route::group(['prefix' => 'agenda', 'middleware' => 'auth', 'permissions' => 'user.create|user.delete'], function() {
//my routes here...
});
和中间件里面:
class AuthMiddleware {
private $r;
private $guard;
public function __construct(Router $r, Guard $g)
{
$this->r = $r;
$this->guard = $g;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$route = $this->r->getCurrentRoute();
$action = $route->getAction(); //$action['permissions'] is the string received from the routes file.
}
文章来源: How to pass arguments from router to middleware in laravel 5?