In the Laravel framework, routing groups are a method to group related routes together and apply common middleware or namespaces. This method helps developers better organize route configurations in a project.
Example: In the code below, we use the prefix method to group routes under the /admin path. This means that all these routes will start with the /admin prefix in their URLs.
Route::prefix('admin')->group(function () {
Route::get('/', 'AdminController@index');
Route::get('/users', 'AdminController@users');
});
With the above code, all routes under /admin will automatically have this prefix applied.
Additionally, we can use the middleware method to apply middleware to the entire route group, thus controlling access or executing other logic uniformly.
Route::middleware('auth')->group(function () {
Route::get('/dashboard', 'DashboardController@index');
Route::get('/profile', 'ProfileController@show');
});
In the example above, the auth middleware restricts unauthenticated users from accessing the /dashboard and /profile routes.
Middleware is an important component of the Laravel framework. It allows you to execute specific logic either before or after a request is handled, such as validation, modifying request/response, handling permissions, and more.
Laravel provides several built-in middleware, such as the auth middleware for user authentication and the csrf middleware to protect against cross-site request forgery attacks.
Route::get('/profile', 'ProfileController@show')->middleware('auth');
In this example, the auth middleware ensures that only authenticated users can access the /profile route.
Namespaces help avoid controller name conflicts and allow better code organization. In Laravel, namespaces ensure that controllers with the same name can coexist in different modules or functional areas.
Example: The code below demonstrates how to group routes under the Admin namespace.
Route::namespace('Admin')->group(function () {
Route::get('/dashboard', 'DashboardController@index');
Route::get('/users', 'UserController@index');
});
In this case, DashboardController and UserController are under the Admin namespace, which helps avoid naming conflicts with controllers in other modules.
A subdomain is a part of a main domain, such as api.example.com where api is the subdomain. In Laravel, we can define routes for a specific subdomain using the domain method.
Example: The code below demonstrates how to group routes under the api.example.com subdomain.
Route::domain('api.example.com')->group(function () {
Route::get('/users', 'UserController@index');
Route::post('/users', 'UserController@store');
});
With the above code, only requests to api.example.com/users and api.example.com/users will match these routes.
This article covered routing groups, middleware, namespaces, and subdomain routing configurations in Laravel. These features help developers efficiently manage and organize routes, providing greater flexibility and control over routing configurations.