In Laravel, Route is responsible for determining what happens when a user requests a specific address (URL).
Routing is the bridge between the URL that the user requested and the application logic (Controller, View, Closure Function).
Example:
User → Browser → /about → Route → Controller or View → Response to user
Main route files in routes/ folder:
web.php→ For web requests (with Session and CSRF)api.php→ For APIs (without Session)console.php→ Console routes (Artisan Commands)channels.php→ Channel routes for Broadcast
Route::get('/about', function () {
return 'About Us Page';
});Route::get('/contact', function () {
return view('contact');
});Route::get('/products', [ProductController::class, 'index']);- GET → Get Data
- POST → Send Data
- PUT/PATCH → Update Data
- DELETE → Delete Data
- ANY → Any Method
- MATCH → Multiple Specific Methods
Example:
Route::post('/form', [FormController::class, 'store']);
Route::delete('/product/{id}', [ProductController::class, 'destroy']);Route::get('/user/{id}', function ($id) {
return "User ID: $id";
});Route::get('/user/{name?}', function ($name = 'Guest') {
return "Hello $name";
});Route::get('/product/{id}', function ($id) {
return "Product ID $id";
})->where('id', '[0-9]+');Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');In Blade: Blade dashboard
---
## 7. Route grouping
### 7.1 with Prefix
```php
Route::prefix('admin')->group(function () {
Route::get('/users', [AdminController::class, 'users']);
Route::get('/posts', [AdminController::class, 'posts']);
});
Route::middleware(['auth'])->group(function () {
Route::get('/profile', [UserController::class, 'profile']);
Route::get('/settings', [UserController::class, 'settings']);
});Route::resource('products', ProductController::class);The following routes are created:
- index
- create
- store
- show
- edit
- update
- destroy
Route::fallback(function () {
return view('errors.404');
});php artisan route:cache
php artisan route:clearRouting is one of the most important parts of Laravel that handles requests and responses. By using Routes properly, you can create a clean, secure, and maintainable structure.