π What Are Routes in Laravel?
Routes are the entry points for your web application.
Every HTTP request is mapped to a route, which tells Laravel which code to execute.
Types of Routes:
Web routes β for browser requests
File:routes/web.phpAPI routes β for APIs
File:routes/api.php
π Basic Route Example
Open routes/web.php and add:
Route::get('/', function () {
return "Welcome to Laravel Routing!";
});
getβ HTTP GET request/β URL pathfunction()β callback executed when route is visited
Visit http://127.0.0.1:8000/ β youβll see the message π
πͺ Route Parameters
Dynamic routes can accept parameters:
Route::get('/user/{id}', function ($id) {
return "User ID: ".$id;
});
{id}β route parameter$idβ received in the closure
Example URL: http://127.0.0.1:8000/user/5 β Output: User ID: 5
π· Named Routes
Named routes help you generate URLs dynamically:
Route::get('/profile', function () {
return "This is your profile";
})->name('profile');
Generate URL in Blade:
<a href="{{ route('profile') }}">Profile</a>
π§ Route Groups & Middleware
Group routes for better organization:
Route::middleware(['auth'])->group(function () {
Route::get('/dashboard', function () {
return "Dashboard Page";
});
Route::get('/settings', function () {
return "Settings Page";
});
});
Only authenticated users can access these routes
middleware β provided by Laravel
