Kritim Yantra
Apr 15, 2025
Routing is one of the most foundational aspects of any web application. In Laravel 12, the routing system has been further refined to provide an elegant, flexible, and powerful structure for handling HTTP requests. Whether you're building a web interface or an API, understanding Laravel 12 routes will ensure your application is organized, scalable, and maintainable.
At its core, Laravel 12 routes map incoming HTTP requests to specific actions within your application. This could be a simple response, a closure, or a call to a controller method.
Example:
Route::get('/home', function () {
return 'Welcome home';
});
Routes are primarily defined in two files:
routes/web.php
: For web routes with session state and CSRF protection.routes/api.php
: For stateless API routes, prefixed with /api
by default.Laravel supports multiple HTTP verbs:
Route::get('/user/{id}', function ($id) {
return 'User ID: ' . $id;
});
You can also use:
post
, put
, patch
, delete
match
for multiple verbsany
for all verbsExample:
Route::match(['get', 'post'], '/contact', function () {
return 'Contact page';
});
Capture dynamic values:
Route::get('/user/{id}', ...);
Route::get('/user/{name?}', ...); // Optional parameter
Easily generate URLs:
Route::get('/profile', ...)->name('profile');
route('profile');
Group routes with shared attributes:
Route::middleware(['auth'])->group(function () {
Route::get('/dashboard', ...);
});
Automatically resolve Eloquent models:
Route::get('/users/{user}', function (App\Models\User $user) {
return $user->email;
});
Supports custom keys and nested binding:
Route::get('/posts/{post:slug}', ...);
Route::get('/users/{user}/posts/{post}', ...)->scopeBindings();
Protect routes from abuse:
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
});
Apply using throttle:api
middleware.
Route::prefix('admin')->group(function () {
Route::get('/users', ...);
});
Route::domain('{account}.example.com')->group(function () {
Route::get('/user/{id}', ...);
});
Route::name('admin.')->group(function () {
Route::get('/users', ...)->name('users'); // admin.users
});
Route::redirect('/old', '/new', 301);
Route::view('/welcome', 'welcome', ['name' => 'Taylor']);
Route::fallback(function () {
return 'Page Not Found';
});
<form method="POST" action="/foo">
@csrf
@method('PUT')
</form>
php artisan route:cache
php artisan route:clear
php artisan route:list
Laravel 12 routing is more powerful and intuitive than ever. From basic route definitions to advanced model bindings and rate limiting, it offers everything a modern web developer needs. By mastering these features, you ensure your Laravel apps are robust, secure, and ready for scale.
Happy routing! 🚀
Transform from beginner to Laravel expert with our personalized Coaching Class starting June 9, 2025. Limited enrollment ensures focused attention.
1-hour personalized coaching
Build portfolio applications
Industry-standard techniques
Interview prep & job guidance
Complete your application to secure your spot
Thank you for your interest in our Laravel mentorship program. We'll contact you within 24 hours with next steps.
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google