Introduction
Route prefixes help group similar routes under a common URL part. This keeps your URLs organized and easier to manage.
Route prefixes help group similar routes under a common URL part. This keeps your URLs organized and easier to manage.
Route::group(['prefix' => 'prefix'], function () { // Define routes here });
Route::group(['prefix' => 'admin'], function () { Route::get('/dashboard', function () { return 'Admin Dashboard'; }); });
Route::group(['prefix' => 'api'], function () { Route::get('/users', function () { return 'User list'; }); });
Route::group(['prefix' => 'user'], function () { Route::get('/profile', function () { return 'User Profile'; }); Route::get('/settings', function () { return 'User Settings'; }); });
This example groups blog-related routes under the /blog prefix. So the URLs are /blog/posts and /blog/posts/{id}.
<?php use Illuminate\Support\Facades\Route; Route::group(['prefix' => 'blog'], function () { Route::get('/posts', function () { return 'List of blog posts'; }); Route::get('/posts/{id}', function ($id) { return "Viewing post with ID: $id"; }); });
Route prefixes do not change the route names unless you also use name prefixes.
You can combine prefix with middleware or namespace for better route organization.
Route prefixes add a common URL part to grouped routes.
They help keep URLs clean and organized.
Use them to group related routes like admin, API, or user sections.
Route::prefix() in Laravel?Route::prefix() doesadmin using Route::prefix() in Laravel?Route::prefix('admin')->group(function () { ... });.Route::group(['prefix' => 'admin'], function () { /* routes */ }); uses group() with array instead of prefix(). Route::prefix('admin', function () { /* routes */ }); incorrectly passes closure as second argument to prefix(). Route::group('admin', function () { /* routes */ }); passes string directly to group().DashboardController@index method?Route::prefix('admin')->group(function () {
Route::get('dashboard', [DashboardController::class, 'index']);
});Route::prefix('api', function () {
Route::get('users', [UserController::class, 'index']);
});api/v1 and middleware auth:sanctum. Which of the following is the correct way to do this using chained prefix() and middleware() methods in Laravel?prefix() and middleware() before group() to apply both settings.Route::group(['prefix' => 'api/v1', 'middleware' => 'auth:sanctum'], function () {
Route::get('profile', [ProfileController::class, 'show']);
}); uses array syntax, not chained methods. Route::prefix('api/v1')->middleware('auth:sanctum')->group(function () {
Route::get('profile', [ProfileController::class, 'show']);
}); correctly chains prefix() and middleware() then calls group(). Route::middleware('auth:sanctum')->prefix('api/v1', function () {
Route::get('profile', [ProfileController::class, 'show']);
}); incorrectly passes closure to prefix(). Route::group('api/v1')->middleware('auth:sanctum')->group(function () {
Route::get('profile', [ProfileController::class, 'show']);
}); misuses group() twice with wrong parameters.Pass the test and this pattern moves unknown