Laravel same route, different controller

后端 未结 4 1266
执念已碎
执念已碎 2020-12-19 12:16

I would like to have general home page and a different homepage for logged-in users
I search a lot on google but I can\'t find what to put in my if statement

I t

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-19 12:45

    You should try something like:

    Route::get('/', array('as'=>'home', function(){
        if (!Auth::check()) {
            Redirect::to('home/index'));
        }
        else{
            Redirect::to('user/index'));
        }
    }));
    

    So you are basically redirecting the user based on the Auth check instead of defining an additional route.

    Or use route filters

    Route::filter('authenticate', function()
    {
        if (!Auth::check())
        {
            return Redirect::to('home/index');
        }
    });
    
    Route::get('home', array('before' => 'authenticate', function()
    {
        Redirect::to('user/index');
    }));
    

    http://laravel.com/docs/routing#route-filters

提交回复
热议问题