dscc90150010
2015-01-31 14:02 阅读 169

如果已登录,则在访问登录页面时,Laravel会重定向到用户仪表板

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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

5条回答 默认 最新

  • donglunzai4288 donglunzai4288 2015-01-31 16:52

    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.

    点赞 评论 复制链接分享
  • doukong6031 doukong6031 2015-01-31 16:56

    There's already a filter for what you want, check your filters.php for "guest"

    点赞 评论 复制链接分享
  • doujiong2533 doujiong2533 2015-01-31 19:44

    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'
    ));
    
    点赞 评论 复制链接分享
  • dougong5285 dougong5285 2015-02-01 03:24

    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']);
    }
    
    点赞 评论 复制链接分享
  • duanan5940 duanan5940 2015-11-26 12:54

    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.

    点赞 评论 复制链接分享

相关推荐