【问题标题】:Laravel - Localhost redirected you too many timesLaravel - 本地主机重定向你太多次
【发布时间】:2016-06-17 19:23:45
【问题描述】:

我们大约一年前开发了一个应用程序,但我遇到了一个问题。 登录 chrome 后说:The localhost page isn’t working, localhost redirected you too many times.。 问题在于控制器上的这段代码,当我删除它时,它工作正常。我不知道这有什么问题。

if(Auth::check()){
        $usertype=usertype::where('uid','=',Auth::user()->id)->first();
        if(isset($usertype)){
        if($usertype->type==1){
            $shopcatagory=shopcatagory::where('sid','=',Auth::user()->id)->first();
            $shopsubcatagory=shopsubcatagory::where('sid','=',Auth::user()->id)->first();
            $shopphone=shopphone::where('sid','=',Auth::user()->id)->first();
            $shopaddress=shopaddress::where('sid','=',Auth::user()->id)->first();
            $shopplace=shopplace::where('sid','=',Auth::user()->id)->first();
            if(!isset($shopaddress) || !isset($shopcatagory) || !isset($shopsubcatagory) || !isset($shopphone) || !isset($shopplace)){
                return Redirect::action('HomeController@accountinfo',Auth::user()->id);
            }
        }

        else if($usertype->type==3){
            $shopphone=shopphone::where('sid','=',Auth::user()->id)->first();
            $shopaddress=shopaddress::where('sid','=',Auth::user()->id)->first();
            $shopplace=shopplace::where('sid','=',Auth::user()->id)->first();
            if(!isset($shopaddress) || !isset($shopphone) || !isset($shopplace)){
                return Redirect::action('HomeController@accountinfo',Auth::user()->id);
            }
        }

        else if($usertype->type == 2){
            $userinterests=userinterests::where('uid','=',Auth::user()->id)->first();
            if(!isset($userinterests)){
                return Redirect::action('HomeController@interests');
            }
        }

        }
    }

【问题讨论】:

  • 除了代码如此粘在一起难以阅读之外,很明显存在无限循环。只需通读那个卷心菜并确定哪些部分可能会失败以及您是否处理了该失败(从您制作的沙拉的一瞥中,您的代码可能会在很多地方失败,因此不值得写一个答案来突出显示每个部分)。
  • 发布此内容时,我也有同样的想法,因为这是我们第一次使用 Laravel,现在我无法更改所有内容以使其变得更好。很明显这是一个意大利面条代码,我看到这个感觉很糟糕。
  • @jszobody 将其作为答案发布,以便我将其作为正确答案进行检查。是的,这些检查在那里运行,删除它们解决了问题。
  • @AkmalArzhang 完成!

标签: php laravel laravel-4


【解决方案1】:

在这个 Auth::check 大块中,您可能会重定向到两个不同的路由:accountinfo 和 interests。

因此,如果您陷入无限循环,那么听起来这段代码正在其中一条(或两条)路线上运行!

跟踪它,确保这两个路由都不包含此 Auth::check 代码和重定向。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多