Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.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 Laravel 5.5多用户类型嵌套路由组_Php_Laravel_Laravel 5.5 - Fatal编程技术网

Php Laravel 5.5多用户类型嵌套路由组

Php Laravel 5.5多用户类型嵌套路由组,php,laravel,laravel-5.5,Php,Laravel,Laravel 5.5,我有两种用户类型:操作和维护。 维护用户类型可以访问操作用户类型的所有路由,但并非所有维护的路由都不能被操作访问 这是我现有的代码 Route::group(['middleware'=>'maintenance'], function(){ //routes here Route::group(['middleware'=>'operations'], function(){ //routes here }); }); kernel.

我有两种用户类型:操作和维护。 维护用户类型可以访问操作用户类型的所有路由,但并非所有维护的路由都不能被操作访问

这是我现有的代码

Route::group(['middleware'=>'maintenance'], function(){
    //routes here

    Route::group(['middleware'=>'operations'], function(){
         //routes here

    });
});
kernel.php

 protected $routeMiddleware = [
    'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'revalidate' => \App\Http\Middleware\RevalidateBackHistory::class,
    'dev' => \App\Http\Middleware\isDev::class,
    'operations' => \App\Http\Middleware\operations::class,
    'maintenance' => \App\Http\Middleware\maintenance::class,
];
中间件/操作.php

public function handle($request, Closure $next)
{
    $a = UserAccess::where(['employee_id'=>\Auth::user()->id, 'user_type_id'=>1])->first();
    if($a){
        return $next($request);
    }

    return redirect('/');
}
但它不起作用。维护人员可以访问其所有路线,但无权访问运营部门,运营部门根本无权访问任何路线


注意:这两个组都在auth中间件组中

我认为您在where查询中遗漏了一个额外的数组,请尝试更改操作中间件中的代码,如下所示:

$a = UserAccess::where([
         ['employee_id', \Auth::user()->id],
         ['user_type_id', 1]
     ])->first();
编辑:

然后尝试将两个中间件移动到web中间件下面的$middleware组中,如下所示:

'web' => [
    ...
],
'operations' => \App\Http\Middleware\operations::class,
'maintenance' => \App\Http\Middleware\maintenance::class,
编辑2:

将$a变量的代码更改为

$a = UserAccess::where([
         ['employee_id', Auth::user()->id], //Hence there is no \ before Auth
         ['user_type_id', 1]
     ])->first();
然后在同一文件的顶部包含以下用法:

编辑3:

您可以向请求中添加其他参数,这些参数随后将被转发,如下所示:

'web' => [
    ...
],
'operations' => \App\Http\Middleware\operations::class,
'maintenance' => \App\Http\Middleware\maintenance::class,
middleware/operations.php

public function handle($request, Closure $next)
{
    $a = UserAccess::where(['employee_id'=>\Auth::user()->id, 'user_type_id'=>1])->first();
    if($a){
        return $next($request);
    }

    return redirect('/');
}
middleware/maintenance.php


找到了一个解决办法。我创建了另一个中间件,通过向该中间件传递两个参数(具体为1=操作和2=维护)来满足操作和维护的需要,并让中间件检查访问。代码如下:

Route::group(['middleware'=>'access:2,2'], function(){
    //Routes that can be accessed by Maintenance user type
});

Route::group(['middleware'=>['access:1,2']], function(){
    //Routes that can be accesed by Operations and Maintenance user type.
});
以下是中间件:

public function handle($request, Closure $next, $ops, $main)
{
    $a = UserAccess::where('employee_id',\Auth::user()->id)->whereIn('user_type_id', [$ops, $main])->first();
    if($a){
        return $next($request);
    }

    return redirect('/');
}
编辑:

优化代码以消除参数冗余

Web.php:

Route::group(['middleware'=>'access:2'], function(){
    //Routes that can be accessed by Maintenance user type
});

Route::group(['middleware'=>['access:1,2']], function(){
    //Routes that can be accesed by Operations and Maintenance user type.
});
access.php

public function handle($request, Closure $next, $ops, $main = 0)
{
    $a = UserAccess::where('employee_id',\Auth::user()->id)->whereIn('user_type_id', [$ops, $main])->first();
    if($a){
        return $next($request);
    }

    return redirect('/');
}

向我们展示您的app/Http/Kernel.php代码以及您的操作中间件code@NikolaGavric代码已被编辑。我认为我的查询没有问题,因为如果我将操作组与维护组分开,它会顺利工作。只有当我将组放入另一个组时,它才会停止工作。嗯,那么您可以尝试将这些中间件实际用作组中间件,而不是单独的中间件,检查相同的输出?它是否抛出任何错误,您能否解释其行为,到底发生了什么?无法访问操作中间件组上的路由。但我可以访问维护中间件上的所有路线。不能访问到底意味着什么?服务器给你401403,或者你得到一个错误,或者你得到一个空白页,或者它将你重定向到某个地方?
public function handle($request, Closure $next, $ops, $main = 0)
{
    $a = UserAccess::where('employee_id',\Auth::user()->id)->whereIn('user_type_id', [$ops, $main])->first();
    if($a){
        return $next($request);
    }

    return redirect('/');
}