Question

i trying to set up a system of route on laravel 4 having like main url example : http://laravel.dev/ and can render 1 of 2 different controller on this route.

example:

if user A is logged i'll show the main page with this url --> http://laravel.dev/

if user A is Not logged i'll show the login page in this url too --> http://laravel.dev/

I tried to set my route like so but it show me a blank page. How can i resolve?

Route::get('/', array('before' => 'detectLang',function(){
    if (Auth::guest()) { // check if user is logged
        Route::get('/', 'MainController@getView'); // function that show the main page
    } else {
        Route::get('/','UserController@getLogin'); // function that render the login page
    }
}));
Was it helpful?

Solution 2

I would recommend removing logic from your routes file as it just complicates matters, instead group routes like so

Route::group(array('before' => 'detectLang'), function() {

    Route::group(array('before' => 'guest'), function() {
        Route::get('/', 'MainController@getView');
    });

    Route::group(array('before' => 'auth'), function() {
        Route::get('/', 'UserController@getLogin');
    });

});

This allows you to group everything in a nice manner and keeps the logic separated, which after all, is the purpose of route filters.

OTHER TIPS

That's not how routing works in Laravel. The closure in a route will be called only if you hit that route, so in this case Laravel will not be able to create those two routes and listen for them.

But you can use a group to do what you need:

Route::group(array('before' => 'detectLang'), function()
{
    if (Auth::guest()) { // check if user is logged
        Route::get('/', 'MainController@getView'); // function that show the main page
    } else {
        Route::get('/','UserController@getLogin'); // function that render the login page
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top