Laravel 5 redirect loop error

2019-04-05 00:29发布

I trying to make a login and admin script, the problem is that I have a redirect loop I dont know why.

I want the login users and can be in the / path not /home.

If change return new RedirectResponse(url('/')); to return new RedirectResponse(url('/anotherpage')); it works but I want to be /

Routes:

    Route::get('/', [
        'as' => 'home', 'uses' => 'HomeController@index'
    ]);


    // Tutorials Routes

    Route::get('/tutorials', 'HomeController@tutorials');
    Route::get('/tutorials/{category?}', 'HomeController@tutorialsCategory');
    Route::get('/tutorials/{category?}/{lesson?}', 'HomeController@tutorialsLesson');

    // Courses and Series Routes

    Route::get('/courses-and-series', 'HomeController@coursesandseries');

    // Admin Routes

    Route::group(['middleware' => 'App\Http\Middleware\AdminMiddleware'],                 function()
    {
        Route::get('/admin', function()
        {
            return 'Is admin';
        });
    });

    Route::controllers([
        'auth' => 'Auth\AuthController',
        'password' => 'Auth\PasswordController',
    ]);

Admin middleware:

    public function handle($request, Closure $next)
    {
        if (Auth::user()->type != 'Admin')
        {
            return abort(404);
        }

        return $next($request);
    }

RedirectIfAuthenticated:

    public function handle($request, Closure $next)
    {
        if ($this->auth->check())
        {
            return new RedirectResponse(url('/'));
        }

        return $next($request);
    }

Home Controller:

class HomeController extends Controller {
    public function __construct()
    {
        $this->middleware('guest');
    }

    public function index()
    {
        return view('home');
    }

    public function tutorials()
    {
        return view('pages.tutorials');
    }

    public function tutorialsCategory()
    {
        return view('pages.tutorials');
    }

    public function tutorialsLesson()
    {
        return view('pages.single');
    }

    public function coursesandseries()
    {
        return view('pages.coursesandseries');
    }

    public function single()
    {
        return view('pages.single');
    }
}

1条回答
Luminary・发光体
2楼-- · 2019-04-05 00:58

You are having these redirection loops because all the methods in HomeController are protected by Guest Middleware.

Since you wish to redirect authenticated users to HomeController@index

Remove $this->middleware('guest'); from HomeController

or

Modify the Guest Middleware to ignore index method

$this->middleware('guest', ['only' => ['tutorials','tutorialsCategory']])

List other methods you wish to protect with Guest Middleware excluding Index method

查看更多
登录 后发表回答