Laravel:ERR_TOO_MANY_REDIRECTS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46435866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:43:47  来源:igfitidea点击:

Laravel: ERR_TOO_MANY_REDIRECTS

phplaravel

提问by

I'm trying to code a basic authentication system in Laravel, its resulting in a redirect loop.

我正在尝试在 Laravel 中编写一个基本的身份验证系统,这导致了重定向循环。

First I just visit the "localhost" domain, it then sends me to /home when I'm not even authenticated and throws the chrome error.

首先,我只是访问“localhost”域,然后当我什至没有通过身份验证时它会将我发送到 /home 并引发 chrome 错误。

Routes:

路线:

Route::group(['middleware' => 'platform', 'namespace' => 'Ec9'], function() {

    Route::group(['prefix' => 'platform'], function() {
        Route::get('/restricted', ['as' => 'platform.restricted', 'uses' => 'PlatformController@getRestrictedView']);
        Route::get('/unlock_route', ['as' => 'platform.unlock_route', 'uses' => 'PlatformController@getUnlockRouteView']);
    });

    Route::group(['domain' => 'localhost', 'midleware' => 'frontend', 'namespace' => 'Frontend'], function() {

        Route::group(['middleware' => 'guest', 'namespace' => 'Guest'], function() {

            Route::group(['prefix' => 'setup'], function() {
                Route::any('/language', ['as' => 'frontend.guest.setup.language', 'uses' => 'LandingController@getSetupLanguageView']);
                Route::any('/welcome', ['as' => 'frontend.guest.setup.welcome', 'uses' => 'LandingController@getSetupWelcomeView']);
            });

            Route::get('/', ['as' => 'frontend.guest.landing', 'uses' => 'LandingController@redirect']);

            Route::get('/login', ['as' => 'frontend.guest.login', 'uses' => 'LoginController@getLoginView']);
            Route::post('/login', ['as' => 'frontend.guest.login', 'uses' => 'LoginController@onLoginPost']);

        });

        Route::group(['middleware' => 'auth', 'namespace' => 'User'], function() {
            Route::get('/home', ['as' => 'frontend.guest.login', 'uses' => 'HomeController@getHomeView']);

            Route::group(['prefix' => 'account'], function() {
                Route::get('/logout', ['as' => 'frontend.user.account.logout', 'uses' => 'AccountController@logout']);
                Route::get('/settings', ['as' => 'frontend.user.account.settings', 'uses' => 'AccountController@getAccountSettingsView']);
            });
        });

    });

});

LandingController:

登陆控制器:

<?php
namespace App\Http\Controllers\Ec9\Frontend\Guest;

use App\Http\Controllers\Controller;
use Redirect;
use Auth;

class LandingController extends Controller
{
    public function redirect() {
        if (Auth::check()) {
            return redirect()->route('frontend.user.home');
        }

        return redirect()->route('frontend.guest.login');
    }
}

And the LoginController and HomeController functions just return the views, I thought it was just spamming the question with code too much if I posted the whole class, so I just explained what it did to paint a picture

而 LoginController 和 HomeController 函数只是返回视图,我认为如果我发布了整个类,这只是用代码过多地发送问题,所以我只是解释了它为绘制图片所做的工作

采纳答案by ggg

You have the name of your home route, the same as your login route.

您有您的家乡路线的名称,与您的登录路线相同。

Route::get('/home', ['as' => 'frontend.guest.login', 'uses' => 'HomeController@getHomeView']);

You can fix this by giving it its own name, something like this.

你可以通过给它自己的名字来解决这个问题,就像这样。

Route::get('/home', ['as' => 'frontend.user.home', 'uses' => 'HomeController@getHomeView']);