In Laravel, you can define a route with a prefix using the prefix
method. Here's how you can do it:
Open the routes/web.php
file in your Laravel project.
Use the Route::prefix
method to define the route with a prefix. The prefix parameter should be the desired prefix for the route.
Route::prefix('admin')->group(function () {
Route::get('dashboard', 'AdminController@index');
Route::get('users', 'AdminController@users');
});
In this example, the prefix 'admin'
is added to the routes. So the URLs for the defined routes will be /admin/dashboard
and /admin/users
.
You can also nest prefixes if needed:
Route::prefix('admin')->group(function () {
Route::prefix('settings')->group(function () {
Route::get('general', 'AdminController@generalSettings');
Route::get('security', 'AdminController@securitySettings');
});
});
In the above example, the routes will have the URLs /admin/settings/general
and /admin/settings/security
.
By using the prefix
method, you can easily define routes with a common prefix and organize your route file.