Laravel 5 redirect loop error

13,139

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

Share:
13,139

Related videos on Youtube

Admin
Author by

Admin

Updated on June 06, 2022

Comments

  • Admin
    Admin almost 2 years

    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');
        }
    }