Laravel Basic
Laravel Form
Laravel Database
Laravel Advance
Laravel’s routing system (as of version 12) is powerful and flexible, allowing you to map URLs to specific actions in an expressive manner. Here's a complete breakdown:
web.php (routes\web.php)
Defines how the application responds to HTTP verbs like GET, POST, PUT, DELETE, PATCH, OPTIONS.
Route::get('/', function () { return view('welcome'); });
welcome.blade.php (resources\views\welcome.blade.php)
<!DOCTYPE html> <html lang="en"> <head> <title>Laravel</title> </head> <body> </body> <h1>Welcome to Laravel</h1> <p>This is a simple welcome page.</p> </body> </html>
routes/web.php
: For web routes with stateful features like sessions and CSRF protections (uses web
middleware group).routes/api.php
: For stateless API endpoints, prefixed with /api
and comes with rate limiting and api
middleware.Redirect Routes: Quickly redirect one route to another.
Route::redirect('/here', '/there');
View Routes: Serve a Blade view without a controller method.
Route::view('/welcome', 'welcome'); (or) Route::get('/welcome', function () { return view('welcome'); });
Capture dynamic values from the URL:
Route::get('/users/{id}', function ($id) { return "User ID: " . $id; });
Output:
Route::get('/users/{name?}', function ($name = null) { return "User name: " . ($name ?? 'no name'); });
Output with name | Output without name |
---|---|
![]() |
![]() |
Route::get('/users/{id}', function ($id) { return "User ID: " . $id; })->where('id', '[0-9]+');
Output with id(0-9) | Output with id(a) |
---|---|
![]() |
![]() |
Gives routes a name for easier referencing elsewhere:
Route::get('/users/{id}', function ($id) { return "User ID: " . $id; })->name('user.profile');
Can access
<a href="{{ route('user.profile', 5) }}"> John Deo </a>
Group your routes for common properties like prefixes:
Route::prefix('admin')->name('admin.')->group(function () { Route::get('/dashboard', function(){ return view('dashboard') })->name('dashboard'); });
Can access:
http://my-app.test/admin/dashboard (Or) <a href="http://my-app.test/admin/dashboard"> Dashboard </a> (Or) <a href="{{ route('admin.dashboard') }}"> Dashboard </a>
Catch-all route for unmatched URIs:
Route::fallback(function () { return view('errors.404'); });
Define custom rate limiters and attach them to routes:
use Illuminate\Cache\RateLimiting\Limit; use Illuminate\Support\Facades\RateLimiter; RateLimiter::for('uploads', function ($request) { return Limit::perMinute(10); }); Route::post('/upload', ...)->middleware('throttle:uploads');
To view all registered routes:
php artisan route:list
Add --columns for filtering:
php artisan route:list --method=GET // Filter by method: php artisan route:list --name=dashboard // Filter by route name: php artisan route:list --path=api/user // Filter by URI path: php artisan route:list --sort=uri // Sort routes by URI: php artisan route:list --reverse // reverse route list