【问题标题】:Same route url use to multiple middlewares in laravel相同的路由 url 用于 laravel 中的多个中间件
【发布时间】:2020-08-02 05:57:51
【问题描述】:

我有两个两个中间件。一个是管理员,另一个是老师。在 admin 中,将访问所有创建的 url,而教师只会获得 2 或 3 个 url。

这是我的路线

 Route::group(['middleware' => ['adminAuth']], function () {
    Route::get('dashboard', array('as' =>'Teacher Dashboard', 'uses' => 'UserController@dashBoard'));
    Route::get('users/profile/edit/{id}', array('as' => 'User Profile Update', 'uses' => 'UserController@userProfile'));
    Route::post('users/profile/update/{id}', array('as' => 'User Profile Update', 'uses' => 'UserController@updateUserProfile'));

    Route::get('student/leave/application', array('as' => 'Student Leave Application', 'uses' =>'LeaveApplicationController@studentLeaveApplicationList'));
    Route::get('leave/application/student/create', array('as' => 'Student Leave Application', 'uses' =>'LeaveApplicationController@studentLeaveApplicationCreate'));
    Route::post('leave/student/application/store', array('as' => 'Student Leave Application', 'uses' =>'LeaveApplicationController@studentLeaveApplicationStore'));
    Route::get('leave/student/application/categories', array('as' => 'Student Leave Application Categories', 'uses' => 'LeaveApplicationController@studentLeaveCategories'));

});
Route::group(['middleware' => ['teacherAuth']], function () {
    Route::get('teacher/dashboard', array('as' =>'Teacher Dashboard', 'uses' => 'UserController@teacherDashBoard'));
    Route::get('users/profile/edit/{id}', array('as' => 'User Profile Update', 'uses' => 'UserController@userProfile'));
    Route::post('users/profile/update/{id}', array('as' => 'User Profile Update', 'uses' => 'UserController@updateUserProfile'));
});

我想从两个中间件更新每个用户配置文件。当我为任何中间件使用配置文件更新 url 时,它工作正常,但是当我在两个中间件中使用配置文件更新 url 时,它不会只是重定向到另一个 url 这是我的中间件逻辑

对于管理员,中间件/AdminAuth.php

 public function handle($request, Closure $next)
{
    $role = User::getUserById(Auth::id());
    if(!(\Auth::check()) || ($role->role_name != "admin"))
    {
        if ($request->ajax())
        {
            return response('Unauthorized.', 401);
        } else {
            \Session::flash('errormessage','Invalid Request');
            \Session::put('pre_login_url',\URL::current());
            return redirect()->guest('/auth/login');
        }
    }
    return $next($request);
}

对于教师,中间件/TeacherAuth.php

 public function handle($request, Closure $next)
{
    $role = User::getUserById(Auth::id());
    if(!(\Auth::check()) || ($role->role_name != "teacher"))
    {
        if ($request->ajax())
        {
            return response('Unauthorized.', 401);
        } else {
            \Session::flash('errormessage','Invalid Request');
            \Session::put('pre_login_url',\URL::current());
            return redirect()->guest('/auth/login');
        }
    }
    return $next($request);
}

这是我的 Kernel.php

 'adminAuth'=>\App\Http\Middleware\AdminAuth::class,
 'teacherAuth'=>\App\Http\Middleware\TeacherAuth::class,

【问题讨论】:

    标签: php laravel laravel-5 middleware


    【解决方案1】:

    Laravel 对路由使用模式匹配,它会选择找到的第一个。中间件不会改变路由路径,所以 laravel 只会识别第一个 users/profile/update/{id} 路由。

    您要么更改路由路径,使它们不完全相同,要么您去分离控制器方法中的逻辑。例如,在您的 UserController::updateUserProfile() 方法中,您可以创建私有方法 updateTeacher()、updateAdmin()。所以你的逻辑可以是这样的:

    if($role->role_name == "teacher")
        {
          return $this->updateTeacher();
    } else if($role->role_name == "admin")
    {
        return $this->updateAdmin();
    }
    

    这意味着您不需要这两个中间件。只需在路由上应用auth中间件

    【讨论】:

      猜你喜欢
      • 2020-10-20
      • 2017-05-03
      • 2018-10-20
      • 1970-01-01
      • 2021-08-05
      • 1970-01-01
      • 2015-02-10
      • 2017-12-07
      • 2021-05-29
      相关资源
      最近更新 更多