Home > Net >  Laravel 8 -Redirecting to login page when i use multiple route with '/'
Laravel 8 -Redirecting to login page when i use multiple route with '/'

Time:02-01

Her i have / routes with auth and without auth. When i tried without auth, the page is redirecting to login page. ie, the route inside auth is called instead of the first one.

Route::get('/', function () {
        return view('welcome');
});


Auth::routes();

/* Authenticated User Routes */
Route::group(['middleware' => 'auth'], function () {
        Route::get('/', [HomeController::class, 'index'])->name('home');
});

CodePudding user response:

If there are 2 routes using the same url Laravel accepts the last one.

If you need to use different routes for login and not logged in situations i can propose you a solution as the example.

if(!auth()->check()){
        Route::get('/', function () {
            return view('welcome');
        })->name('home');
}
Auth::routes();

/* Authenticated User Routes */
Route::group(['middleware' => 'auth'], function () {
        if(auth()->check()){
              Route::get('/', [HomeController::class, 'index'])->name('home');
        }
});
  •  Tags:  
  • Related