Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 多个中间件在相同的路由上具有权限_Php_Laravel_Routes_Laravel 5.1 - Fatal编程技术网

Php 多个中间件在相同的路由上具有权限

Php 多个中间件在相同的路由上具有权限,php,laravel,routes,laravel-5.1,Php,Laravel,Routes,Laravel 5.1,我有多个中间件(studen、parent、admin),并使用该中间件创建一些路由组。但是,如果用户在其中任何一个组中并且属于该中间件中的任何一个,则某些路由可以访问,但如果用户在其他中间件中,则不能访问。我在文档中使用了类似的东西:但当我放置一个路由时,它是有效的,当用另一个中间件添加另一个路由组时,它不起作用。这可能吗?如何做到 当我执行php artisan路由时,它会给我一个错误 [Symfony\Component\Debug\Exception\FatalErrorExceptio

我有多个中间件(studen、parent、admin),并使用该中间件创建一些路由组。但是,如果用户在其中任何一个组中并且属于该中间件中的任何一个,则某些路由可以访问,但如果用户在其他中间件中,则不能访问。我在文档中使用了类似的东西:但当我放置一个路由时,它是有效的,当用另一个中间件添加另一个路由组时,它不起作用。这可能吗?如何做到

当我执行php artisan路由时,它会给我一个错误

[Symfony\Component\Debug\Exception\FatalErrorException]
  Call to a member function inRole() on null

Laravel的路由中间件按照routes.php文件中的声明逐一执行。因此,如果其中一个通过抛出异常或返回一些响应来拒绝访问,那么下一个中间件将不会执行

为了实现这一点,您需要一个中间件来检查当前用户是否具有任何所需的角色。幸运的是,从laravel5.1开始,您可以从routes.php文件向中间件传递参数(请参阅),因此您只需要一个中间件类来处理所有情况

示例中间件类可能如下所示:

class HasAnyRole
{
  public function handle($request, Closure $next, $roles)
  {
    // Return Not Authorized error, if user has not logged in
    if (!$request->user) {
      App::abort(401);
    }

    $roles = explode(',', $roles);
    foreach ($roles as $role) {
      // if user has given role, continue processing the request
      if ($request->user->hasRole($role)) {
        return $next($request);
      }
    }
    // user didn't have any of required roles, return Forbidden error
    App::abort(403);
  }  
}
//this route is available only to users with role admin or author
Route::put('post/{id}', ['middleware' => 'has_any_role:admin,author', function ($id) {
    //
}]);
Kernel.php中注册中间件:

protected $routeMiddleware = [
  'has_any_role' => 'App\Http\Middleware\HasAnyRole',
];
现在,在您的routes.php中,您可以将中间件应用于如下组:

class HasAnyRole
{
  public function handle($request, Closure $next, $roles)
  {
    // Return Not Authorized error, if user has not logged in
    if (!$request->user) {
      App::abort(401);
    }

    $roles = explode(',', $roles);
    foreach ($roles as $role) {
      // if user has given role, continue processing the request
      if ($request->user->hasRole($role)) {
        return $next($request);
      }
    }
    // user didn't have any of required roles, return Forbidden error
    App::abort(403);
  }  
}
//this route is available only to users with role admin or author
Route::put('post/{id}', ['middleware' => 'has_any_role:admin,author', function ($id) {
    //
}]);

这应该可以做到,只要确保你的用户类有一个hasRole方法。

请将你的中间件代码粘贴到运行inRole()methodTNX@jedrzej.kurylo的地方。这是我得到的最好的答案!非常感谢。在列出路由时仍然有错误,可能是因为我使用了sentinel包,但我并没有看到错误,但我试图找出它。TNX.没关系,我发现了一个问题…在我使用的if(Sentiel::hasRole('student')控制器中,当我把if(!Sentiel::gest())放在前面时,它是有效的。谢谢@jedrzej.kurylo。你是最好的!