简体   繁体   中英

Laravel redirect to user dashboard when visiting login page if logged in

I have a basic login system setup, but I would like the user to be sent to there dashboard page if they try to access the "login" page or "create account" page.

How do I go about doing this?

I am thinking something in the routes file?:

Route::post('/login', array('uses' => 'UserController@login'));
Route::post('/create-account', array('uses' => 'UserController@createAccount'));

Route::group(array('before' => 'auth'), function () {
    Route::get('/dashboard', array('uses' => 'DashboardController@index'));

    Route::get('/logout', function () {
        Auth::logout();
        return Redirect::to('/start');
    });
});

Perhaps some kind of group around the first two routes?

A before filter is perfect for this. Since it basically will do the opposite of auth let's call it no_auth :

Route::filter('no_auth', function(){
    if(Auth::check()){
        return Redirect::to('dashboard');
    }
}

And then wrap a group around your two routes to apply the filter:

Route::group(array('before' => 'no_auth'), function(){
   Route::post('/login', array('uses' => 'UserController@login'));
   Route::post('/create-account', array('uses' => 'UserController@createAccount'));
});

Edit

Actually, as @afarazit points out, there's already a filter like that in app/filters.php called guest . You just have to change the redirect URL to dashboard and you're ready to go.

已经有您想要的过滤器 ,请检查您的filters.php中的“ guest”

There are many ways to do this. You can use an Event listener like so:

Event::listen('user.login', function (){
    if(Auth::check()){
        return Redirect::to('dashboard');
    }
});


Event::listen('user.create', function (){
    if(Auth::check()){
        return Redirect::to('dashboard');
    }
});

You need a named controller for above like so:

Route::post('/login', array(
    'uses' => 'UserController@login',
    'as' => 'user.login'
));

Route::post('/create-account', array(
    'uses' => 'UserController@createAccount',
    'as' => 'user.create'
));

You may use a constructor and include a filter in it. Here is an example; You can modify your code according to the example.

public function __construct(SignInForm $signInForm)
{
    $this->signInForm = $signInForm;
    $this->beforeFilter('guest', ['except' => 'destroy']);
}

If laravel version is 4.2

open your app/filters.php and add

Route::filter('no_auth', function(){
    if(Auth::check()){
        return Redirect::to('dashboard');
    }
});

add your login and create account pages to your app/routes.php like below:

Route::group(array('before' => 'no_auth'), function(){
    Route::post('/login', array('uses' => 'UserController@login'));
    Route::post('/create-account', array('uses' => 'UserController@createAccount'));

});

It worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM