简体   繁体   中英

After using middleware the redirection to a url is showing "This page isn't working". But when I remove the middleware the page is displayed

The code for middleware in app/http/Middleware/RoleMiddleware.php

  public function handle(Request $request, Closure $next)
{
    if(Auth::user()->role == 3){
        return redirect('sales');
    }
   return redirect('login');
}

The route is defined in route/web.php

Auth::routes();    
route::get('/sales',[App\Http\Controllers\DashboardController::class,'ShowSales']);

The DashboardController Contains following code to view the page if the remove the constructor then the page is displayed in the browser but when the constructor with the middleware role is added I am getting error such as the "This page isn't working the page redirected you too many times."

 public function __construct()
{
    $this->middleware('role');
}
 public function ShowSales(){
        $data = [
            'category_name' => 'dashboard',
            'page_name' => 'sales',
            'has_scrollspy' => 0,
            'scrollspy_offset' => '',
        ];
        return view('dashboard')->with($data);
    }

And the dashboard.blade.php is created in views/dashboard.blade.php which contained html code.

The middleware code was wrong and it is making it to loop multiple times through sales route. Below code works.

public function handle(Request $request, Closure $next)
{
    if(Auth::user()->role == 3){
         return $next($request);
    }
   return redirect('login');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM