1
votes

I am having an issue with laravel routing.

I want to have routes like this:

/  - home page for unauthenticated users
/login  - login page
/register  - register page
/dashboard  - home page for authenticated users

After login i want user to be redirected to /dashboard, and if authenticated user goes to / or any other unprotected route, i also want to redirect him to /dashboard.

My routes.php.

`Route::get('/', 'HomeController@index');
 Route::group(['middleware' => 'web'], function () {


    Route::auth();

    Route::get(‘/dashboard’, ‘DashboardController@index');
    Route::get('/logout', 'Auth\AuthController@logout');


});`

This works, however if authenticated user goes to / or any other unprotected route, i would like to redirect him to /dashboard. How can i make this work?

3
In your HomeController@index method, do a check and redirect the Auth user to dashboard. Auth::check() ? return redirect()->url('/dashboard') : ''; - Jilson Thomas
Thanks, it works now. - John
I'll post it as answer - Jilson Thomas

3 Answers

1
votes

Taken from Laravel docs.

Path Customization

When a user is successfully authenticated, they will be redirected to the / URI. You can customize the post-authentication redirect location by defining a redirectTo property on the AuthController:

protected $redirectTo = '/home';

When a user is not successfully authenticated, they will be redirected back to the login form location automatically.

See more here. https://laravel.com/docs/5.2/authentication#included-routing

1
votes

you need to set ::

protected $redirectTo = '/home'

in the AuthController which will override the $redirectTo variable in the Trait used by AuthController.

u can also change redirectAfterLogout url in the same way.

!!Happy Coding.

0
votes

In your HomeController@index method, do a check and redirect the Auth user to dashboard. Auth::check() ? return redirect()->url('/dashboard') : '';