Comprehensive Guide to Laravel Routing: Definitions, Parameters, Groups, Middleware, Prefixes, Sub‑domains, Namespaces, and Caching
This article provides an in‑depth tutorial on Laravel routing, covering basic route definitions, HTTP verbs, parameter handling, regex constraints, route naming, grouping, middleware, prefixes, sub‑domains, namespaces, and cache management, complete with code examples for each feature.
Laravel routing enables developers to map HTTP requests to controller actions or closures using the Route facade. Basic route definitions use methods such as get, post, put, patch, delete, and options. Route::get($uri, $callback); Common route actions can be defined succinctly, and multiple HTTP verbs can be combined with any or match methods.
Route::any(['get','post'], '/', function () { return view('welcome'); });Parameters can be added to routes, optionally marked with a ? for optional values, and constraints can be applied using where with regular expressions.
Route::get('user/{id?}', function ($id = 1) { return "User ID: $id"; });
Route::get('page/{id}', function ($id) { return "Page ID: $id"; })->where('id', '[0-9]+');Routes can be given names for easier URL generation, using the name method.
Route::get('user/{id?}', function ($id = 1) { return "User ID: $id"; })->name('user.profile');Route groups allow sharing attributes such as middleware, prefixes, domains, and namespaces across multiple routes.
Route::middleware('auth:api')->group(function () {
Route::get('dashboard', function () { return view('dashboard'); });
Route::get('account', function () { return view('account'); });
});Prefix groups prepend a URI segment to all contained routes.
Route::prefix('api')->group(function () {
Route::get('/', function () { /* /api */ });
Route::get('users', function () { /* /api/users */ });
});Domain groups bind routes to specific sub‑domains, allowing dynamic placeholders.
Route::domain('{account}.blog.test')->group(function () {
Route::get('/', function ($account) { /* handle */ });
Route::get('user/{id}', function ($account, $id) { /* handle */ });
});Namespaces simplify controller references within a group.
Route::namespace('Admin')->group(function () {
Route::get('/admin', 'AdminController@index');
});Laravel provides commands to cache and clear route definitions for performance optimization.
php artisan route:cache
php artisan route:clearOverall, the guide demonstrates how to define, organize, and manage Laravel routes effectively, covering all essential features needed for backend development.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
