简体   繁体   中英

Laravel same route different controllers for auth and guest users

I am trying to have the same route homepage / but different controllers for auth and guest users but i am unable to do this. I have searched and tried all the results on google, stackoverflow etc, none have worked. I am guessing it is because of the version i am using Laravel Framework 7.15

 $uses = 'BlogController@index';
 if (!is_null(auth()->user())) {
     $uses = 'HomeController@index';

 }
 Route::get('/', $uses);

BlogController is for guest and HomeController is for authenticated users. So when i run the code for authenticated users it shows only shows the Blog(guest) homepage and not users HomeController page. Thanks for your help in advance.

You can try this.

Route::get('/', (function() {
    return auth()->user()
        ? app()->make(\App\Http\Controllers\HomeController::class)->index()
        : app()->make(\App\Http\Controllers\BlogController::class)->index();
}));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM