Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Comprehensive Guide to Laravel Routing: Definitions, Parameters, Groups, Middleware, Prefixes, Sub‑domains, Namespaces, and Caching

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:clear

Overall, the guide demonstrates how to define, organize, and manage Laravel routes effectively, covering all essential features needed for backend development.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Report an issue with this article
Backendmiddlewareroutingweb developmentPHPLaravelRoute Grouping

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion