Laravel Route Groups, Prefixes & Controller Groups
Your invoicing app now has an admin area. The admin can see all invoices, open one invoice, list clients, and view GST reports. Every one of these URLs starts with /admin, and every route name starts with admin..
If you write each route separately, you type /admin and admin. over and over. Later, when the client says, “Please change /admin to /staff”, you edit twenty lines and hope you did not miss one. This is the same maintenance problem that named routes solved for links, now happening inside the routes file itself.
Route groups fix this. You declare what the routes share, such as a URL prefix, a name prefix or a controller, and then list only what is different about each route.
The Problem: Repeating Yourself
Here is what the admin routes look like without groups:
Route::get('/admin/invoices', fn () => 'Admin invoices')->name('admin.invoices.index');
Route::get('/admin/invoices/{invoice}', fn (string $invoice) => "Invoice {$invoice}")
->whereNumber('invoice')
->name('admin.invoices.show');
Route::get('/admin/clients', fn () => 'Admin clients')->name('admin.clients.index');
Route::get('/admin/reports/gst', fn () => 'GST report')->name('admin.reports.gst');
This works, but /admin appears four times and admin. appears four times. Add ten more routes and the repetition becomes a real risk. One typo, like /amdin/clients, creates a route that nobody can find.
Route Groups with prefix()
The prefix() method adds the same text to the start of every URL inside the group:
use Illuminate\Support\Facades\Route;
Route::prefix('admin')->group(function () {
Route::get('/invoices', fn () => 'Admin invoices');
Route::get('/clients', fn () => 'Admin clients');
});
The first route now responds at /admin/invoices and the second at /admin/clients. The code inside the closure runs once, when Laravel loads your routes. It does not run on every request.
You do not need to write the /admin part again inside the group. Writing it again is a common beginner mistake, and you will see it in the Common Mistakes section.
Route Name Prefixes with name()
A prefix changes the URL but not the route name. To prefix the names, use name() on the group:
Route::prefix('admin')->name('admin.')->group(function () {
Route::get('/invoices', fn () => 'Admin invoices')->name('invoices.index');
Route::get('/clients', fn () => 'Admin clients')->name('clients.index');
});
Laravel joins the group name and the route name together. The first route is now named admin.invoices.index, and you generate its URL the way you learned in the previous lesson:
route('admin.invoices.index');
// http://localhost:8000/admin/invoices
Notice the dot at the end of 'admin.'. Laravel does not add it for you. It simply glues the two strings together, so you must include the dot yourself.
Putting the Admin Area Together
Here is the full admin section from the problem above, written with a group. The URLs and names are exactly the same, but the repeated parts are gone:
Route::prefix('admin')->name('admin.')->group(function () {
Route::get('/invoices', fn () => 'Admin invoices')
->name('invoices.index');
Route::get('/invoices/{invoice}', fn (string $invoice) => "Invoice {$invoice}")
->whereNumber('invoice')
->name('invoices.show');
Route::get('/clients', fn () => 'Admin clients')
->name('clients.index');
});
If the client now wants /staff instead of /admin, you change prefix('admin') to prefix('staff'). Every URL updates, and because your links use route(), nothing breaks.
Nested Groups
Groups can sit inside other groups. Each level adds its own prefix and name to the one above it.
Route::prefix('admin')->name('admin.')->group(function () {
Route::prefix('reports')->name('reports.')->group(function () {
Route::get('/gst', fn () => 'GST report')->name('gst');
Route::get('/sales', fn () => 'Sales report')->name('sales');
});
});
The result:
/admin/reports/gst -> admin.reports.gst /admin/reports/sales -> admin.reports.sales
Nesting is useful, but keep it shallow. Two levels are easy to read. Four levels make a reader count prefixes in their head, which defeats the purpose of using a group.
Route Parameters in a Prefix
A prefix can contain a parameter, just like a normal URL. This is perfect when many routes belong to one client:
Route::prefix('clients/{client}')->name('clients.')->group(function () {
Route::get('/invoices', function (string $client) {
return "Invoices of client {$client}";
})->name('invoices.index');
Route::get('/invoices/{invoice}', function (string $client, string $invoice) {
return "Client {$client}, Invoice {$invoice}";
})->name('invoices.show');
});
As you learned in the Route Parameters lesson, values are passed by position. The prefix parameter {client} comes first in the URL, so it is the first argument in your function.
Generating URLs follows the same rules as before:
route('clients.invoices.show', ['client' => 7, 'invoice' => 1001]);
// http://localhost:8000/clients/7/invoices/1001
You can also add a constraint to the whole group, so it applies to every route inside:
Route::prefix('clients/{client}')
->where(['client' => '[0-9]+'])
->name('clients.')
->group(function () {
// every route here only accepts a numeric client
});
Controller Groups
So far the routes used closures. In a real project, routes point to controller methods, and you will build controllers in the Controllers chapter. For now, assume a class InvoiceController exists with the methods index, create and show.
Without a group, you repeat the class name on every route:
use App\Http\Controllers\InvoiceController;
Route::get('/invoices', [InvoiceController::class, 'index'])->name('invoices.index');
Route::get('/invoices/create', [InvoiceController::class, 'create'])->name('invoices.create');
Route::get('/invoices/{invoice}', [InvoiceController::class, 'show'])->name('invoices.show');
With Route::controller(), you name the class once and then use only the method name as a string:
use App\Http\Controllers\InvoiceController;
Route::controller(InvoiceController::class)->group(function () {
Route::get('/invoices', 'index')->name('invoices.index');
Route::get('/invoices/create', 'create')->name('invoices.create');
Route::get('/invoices/{invoice}', 'show')->name('invoices.show');
});
You can combine it with a prefix and a name prefix as well:
Route::controller(InvoiceController::class)
->prefix('invoices')
->name('invoices.')
->group(function () {
Route::get('/', 'index')->name('index');
Route::get('/create', 'create')->name('create');
Route::get('/{invoice}', 'show')->whereNumber('invoice')->name('show');
});
Here the URLs are /invoices, /invoices/create and /invoices/{invoice}, and the names are invoices.index, invoices.create and invoices.show. When you reach the Resource Routes lesson, you will see that Laravel can generate exactly this set of routes with a single line. Groups are still the right tool for routes that do not fit that pattern.
Other Things a Group Can Share
A group can share more than URLs and controllers. The most common extra is middleware, which is code that runs before your route, such as checking that the user is logged in. You will learn how middleware works in the Middleware lessons, but the syntax for sharing it across a group looks like this:
Route::middleware('auth')->prefix('admin')->name('admin.')->group(function () {
Route::get('/invoices', fn () => 'Admin invoices')->name('invoices.index');
});
One line protects every route inside the group. This is the main reason experienced developers use groups so heavily.
Subdomain Routing
If your app serves different companies from different subdomains, such as acme.billing.example.in and sharma.billing.example.in, a group can capture the subdomain as a parameter:
Route::domain('{company}.billing.example.in')->group(function () {
Route::get('/invoices', function (string $company) {
return "Invoices for {$company}";
});
});
The subdomain parameter is passed to your function before any URL parameters. Register subdomain groups above routes for the main domain, so Laravel can match them first. This pattern is common in multi-tenant applications, where one codebase serves many customers.
Loading a Group from a Separate File
When routes/web.php grows too long, you can move a group into its own file and load it with the same options:
// routes/web.php
Route::prefix('admin')
->name('admin.')
->group(base_path('routes/admin.php'));
// routes/admin.php
<?php
use Illuminate\Support\Facades\Route;
Route::get('/invoices', fn () => 'Admin invoices')->name('invoices.index');
Route::get('/clients', fn () => 'Admin clients')->name('clients.index');
The routes in routes/admin.php get the admin prefix and the admin. name prefix automatically. This keeps each file short and focused.
Common Mistakes
Mistake 1: Forgetting the Dot in the Name Prefix
Route::prefix('admin')->name('admin')->group(function () {
Route::get('/invoices', fn () => 'Invoices')->name('invoices.index');
});
The route name becomes admininvoices.index, with no dot. Your link then fails with:
Symfony\Component\Routing\Exception\RouteNotFoundException: Route [admin.invoices.index] not defined.
Write name('admin.') with the dot at the end.
Mistake 2: Repeating the Prefix Inside the Group
Route::prefix('admin')->group(function () {
Route::get('/admin/invoices', fn () => 'Invoices');
});
The real URL is now /admin/admin/invoices. Visiting /admin/invoices returns a 404, and no error explains why. The prefix is added for you, so write only /invoices inside the group.
Mistake 3: Dynamic Routes Before Fixed Routes
The ordering trap from the Route Parameters lesson still applies inside a group:
Route::prefix('invoices')->group(function () {
Route::get('/{invoice}', fn (string $invoice) => "Invoice {$invoice}");
Route::get('/create', fn () => 'Create form');
});
Visiting /invoices/create shows Invoice create, because {invoice} matches first. Put /create above /{invoice}, and add whereNumber('invoice') so the dynamic route only accepts digits.
Mistake 4: Nesting Groups Too Deeply
A group inside a group inside a group, each adding a prefix, a name and middleware, is hard to read and hard to debug. If you need to work out the final URL by adding up four lines, split the routes into separate files, or use a shorter structure. A group should make your routes easier to understand, not harder.
Quick Summary
- Route groups let many routes share the same URL prefix, name prefix, controller, middleware or domain.
Route::prefix('admin')adds text to the start of every URL in the group.->name('admin.')adds text to the start of every route name. Include the trailing dot yourself.- Groups can be nested, and each level adds its own prefix and name.
- A prefix can contain a parameter, like
clients/{client}, and the prefix parameter comes first in your function arguments. Route::controller(InvoiceController::class)lets you write only the method name on each route.- A group can also share middleware, for example
Route::middleware('auth'). Route::domain()captures subdomains as parameters for multi-tenant apps.- A group can load routes from a separate file with
->group(base_path('routes/admin.php')). - Do not repeat the prefix inside the group, and keep fixed routes above dynamic ones.
What’s Next
Your routes are now organised, named and constrained, but a URL like /invoices/1001 still gives you only the number 1001, and you have to fetch the invoice yourself. In the next lesson, Route Model Binding, you will learn how Laravel can turn that number into the actual invoice record automatically, before your code even runs.