Route groups with default values in Laravel

1

I need to understand how I create routes with default values (it would be the default we have in the switch function of PHP). I'm looking for something like:

Route::group(['prefix' => '/'], ['as' => 'public'], function(){

    Route::get('/{userid}', function () {
        return 'Perfil publico do usuário';
    });

    //rota de exemplo do que estou procurando, esta seria a default
    //caso o usuario não tenha buscado nenhum outro usuario no site
    Route::get('/', function(){
        return 'Página inicial do site'
    });

});

What is the best method to generate this type of route? is there any default function for routing groups?

    
asked by anonymous 22.07.2015 / 15:05

1 answer

1

You can simply put a question mark (?) at the end of {userid} .

So, you set the default value for the parameter variable, which represents the id passed in the url.

Route::get('/{userid?}', function ($userid = null) {
     if ($userId === null) {
         //Lógica para acesso feito apenas com a '/'
     } else {
         Usuario:findOrFail($userId);
         ...
    }
});

So we would have two hits on the same route:

meu.site/

and

meu.site/1
    
22.07.2015 / 15:14