Вопрос

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
    }
}));
Это было полезно?

Решение 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.

Другие советы

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
    }
});
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top