Laravel9 Routing

Basic routing

Basic routing contain URI and closure.


Route::get('/message', function(){
return "Welcome to CAPSCOM TECHNOLOGY";
});

Route for controller and its function

Route::get('/login', [HomeController::class, 'login']);

Passing parameters to controller & its function

Route::get("/register/{username}", [HomeController::class, 'register']);

* Some time we does not have controller functionality and want to load view directly.

Route for view only

Route::view("/contact", "welcome", ["message"=>"Welcome to"]);

Named routes

Route::view("/profile", [ProfileController::class, 'profile'])->name('profile');

Add middleware to routes

Route::middleware('isLogin')->get('profile', [ProfileController::class, 'profile'])->name('profile');

Add middleware to group of routes

Route::middleware('isLogin')->group(function(){
Route::get('/profile', [UserController::class, 'profile']);
Route::get('/logout', [UserController::class, 'logout']);
});

Group of routes utilizing same Controller

Route::controller(ProfileController::class)->group(function(){
Route::get('dashboard', 'dashboard');
Route::get('logout', 'logout');
});

Route prefixes

Route::prefix('admin')->group(function(){
Route::get('users', [DashboardController::class, 'users']);
Route::get('payments', [DashboardController::class, 'payments']);
});
© 2016 - 2022, All Rights are Reserved.