使用 Laravel 嵌套路由组

Utt*_*ara 2 routes nested laravel

我正在为路由组使用中间件,并拥有三个中间件adminteacher并且teacheradmin
管理工作正常,但假设我有 10 条路由,并且所有路由都在组下定义teacheradmin(目前的工作情况)
,但我只想访问这 10 条路由中的 5 条由中间件teacher和所有 10 个由中间件访问teacheradmin

这就是我嵌套路由组的方式

Route::group(['middleware' => 'teacheradmin'], function() {
   //defined 5 routes only accessible by teacheradmin

   Route::group(['middleware' => 'teacher'], function() {
       //defined the other routes accessible by both teacher and teacheradmin
   });
});
Run Code Online (Sandbox Code Playgroud)

但上面的嵌套不起作用,teacheradmin无法访问下定义的路由teacher

请我需要一个关于如何让它发挥作用的指导

更新:
根据答案,我已经为常见路由定义了中间件数组

Route::group(['middleware' => ['teacher', 'teacheradmin']], function() {
   //defined common routes
});
Run Code Online (Sandbox Code Playgroud)

这两个中间件的处理方法是:

老师

public function handle($request, Closure $next)
    {
        if(Auth::check())
        {
            if(Auth::user()->user_type != 'TEACHER')
            {
                return redirect()->route('dashboard');
            }
            return $next($request);
        }
        else
        {
            return redirect('/')
                    ->withErrors('That username/password does not match, please try again !!!.');
        }
    }
Run Code Online (Sandbox Code Playgroud)

教师管理

public function handle($request, Closure $next)
    {
        if(Auth::check())
        {
            if(Auth::user()->user_type != 'TEACHER_ADMIN')
            {
                return redirect()->route('dashboard');
            }
            return $next($request);
        }
        else
        {
            return redirect('/')
                    ->withErrors('That username/password does not match, please try again !!!.');
        }
    }
Run Code Online (Sandbox Code Playgroud)

仪表板路线转到此方法

public function Dashboard(Request $request)
    {
        $user = Auth::user();

        if($user->user_type === 'ADMIN') {
            return redirect()->route('dashboardadmin');
        } else if($user->user_type === 'TEACHER_ADMIN') {
            return redirect()->route('dashboardteacher');
        } else if($user->user_type === 'TEACHER') {
            return redirect()->route('world_selection');
        } else {
            return redirect()->route('dashboardchild');
        }
    }
Run Code Online (Sandbox Code Playgroud)

现在我面临的问题是当我在仪表板上并且我尝试访问公共路线时,teacheradmin它也会处理teacher因此再次返回同一页面

Bjö*_*örn 5

不知道为什么你要嵌套它们。您可以通过数组表示法将多个中间件附加到一个组,如下所示:

Route::group(['middleware' => 'teacheradmin'], function() {
    //defined 5 routes only accessible by teacheradmin
});

Route::group(['middleware' => ['teacher', 'teacheradmin']], function() {
    //defined the other routes accessible by both teacher and teacheradmin
});
Run Code Online (Sandbox Code Playgroud)

更新:

我认为您想要做的事情可以通过仅使用一个带有中间件参数的中间件来完成:

Route::group(['middleware' => 'role:teacheradmin'], function() {
    //defined 5 routes only accessible by teacheradmin
});

Route::group(['middleware' => 'role:teacher,teacheradmin'], function() {
    //defined the other routes accessible by both teacher and teacheradmin
});
Run Code Online (Sandbox Code Playgroud)

在角色中间件中:

public function handle($request, Closure $next, ...$roles)
{
    dd($roles);
    //Do your role checking here
    return $next($request);
}
Run Code Online (Sandbox Code Playgroud)

免责声明:...$roles适用于 php 5.6 以上版本。