简体   繁体   English

在 laravel 中重定向太多次

[英]redirect too many times in laravel

when i enter my /home route then show this problem redirect page too many times .当我输入我的 /home 路由时,会多次显示此问题重定向页面 I created a custom middleware this name UserRole.In this middleware I have put a condition if role==1 then go to root(/) route otherwise it go to /home route.First condition worked properly but else condition throw this problem redirect page too many times .我创建了一个名为 UserRole 的自定义中间件。在这个中间件中,我设置了一个条件,如果 role==1 然后 go 到 root(/) 路由,否则它 go 到 /home 路由。第一个条件正常工作,但其他条件抛出这个问题重定向页面太多次了 My code is given below:我的代码如下:

web.php
Route::get('/', 'FrontendController@FrontPage');
Route::get('/home', 'HomeController@dashboard');

Kernel.php

'user_role' => \App\Http\Middleware\UserRole::class,


UserRole.php(midleware)

public function handle($request, Closure $next)
{
    if(Auth::user()->role == 1){
        return redirect('/');
    }
    else{
        return redirect('/home');
    }
}

Homecontroller.php
  class HomeController extends Controller
 {

 public function __construct()
  {
    $this->middleware('user_role');
  }

 public function dashboard()
  {
    return view('backend.dashboard');
  }
 }

FrontendController.php

class FrontendController extends Controller
{
public function FrontPage(){
    $products=Product::all();
    return view('frontend.main',compact('products'));
}
public function shopPage(){
    $categories=Category::orderBy('name','asc')->get();
    $title='All Product';
    return view('frontend.shop',compact('categories','title'));
 }
}

You have an infinite loop.你有一个无限循环。 When you enter in HomeController you pass by the middleware.当您进入 HomeController 时,您会经过中间件。 This middleware redirect you to the HomeController, the controller call the middleware....该中间件将您重定向到 HomeController,controller 调用中间件....

You can change the middleware like this您可以像这样更改中间件

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

    return $next($request);
}

This middleware check if you're role is 1. If it's true go to '/' If not continue此中间件检查您的角色是否为 1。如果为真 go 为 '/' 如果不继续

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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