php - laravel route callback return controller
one text
Solution:
First things first,
Since you are returning from an if statement, no else is required
This
Route::get('/home', function () {
if (session()->has('username')) return view('home');
else return redirect('/');
});
Should look like this;
Route::get('/home', function () {
if (session()->has('username')) {
return view('home');
}
return redirect('/');
});
Second, dont worry about all that if else inside of the route and just pass it to your controller, throw a route middleware on it. Below is the basic auth middleware but you can also create your own
use App\Http\Controllers\UserController;
Route::get('/home', [HomeController::class, 'index'])->middleware('auth');
You can read up more on middlewares here https://laravel.com/docs/8.x/middleware
Source