Middleware for routing authentication to an existing vector in Laravel

Asked

Viewed 320 times

1

I’m starting to understand Laravel’s routing system now, and I’ve come up with a question regarding the Middleware. Can I have two equal routings for the same vector where when my user is authenticated the return of this routing will 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 not possible, how best to modify a route function when there is a user ?

1 answer

2


You can simply implement a condition 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']);
    }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.