dimanche 19 avril 2020

Same route url use to multiple middlewares in laravel

I have two two middlewares. One is admin and another is teacher. In admin, will access all the created url and teacher will get only 2 or 3 url.

Here is my route

 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'));
});

I want to update each user profile from both middleware. it is working fine when i use profile update url for anyone middleware but when i use profile update url in both middleware then it not wokring just redirect to another url Here is my middlewares logic

For Admin,Middleware/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);
}

For Teacher, Middleware/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);
}

Here is my Kernel.php

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


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire