Middleware for authentication by routing to an existing vector in Laravel

1

I am now beginning to understand Laravel's routing system, and I have a question about Middlewares. Can I have two equal routing for the same vector where, when my user is authenticated, will the return of this routing be different? Example:

Route::get('/', function () {
    return 'Página Inicial';
});
Route::group(['middleware' => 'auth'], function () {
    Route::get('/', function () {
        return 'Painel de controle do usuário autenticado';
    });
});

If this is not possible, what is the best way to change the function of a route when a user exists?

    
asked by anonymous 22.07.2015 / 17:04

1 answer

2

You can simply implement a conditional that will check whether the user is authenticated or not and return the controller or its related function.

Example:

Route::group(['prefix' => '/'], function()
{
    if(Auth::check()){
        Route::any('/', ['as' => 'index', 'uses' => 'IndexController@index']);
    }
    else{
        Route::any('/', ['as' => 'panel', 'uses' => 'PanelController@index']);
    }
});
    
22.07.2015 / 18:38