Introduction
In Laravel 11, route and middleware registration have been modernized with a cleaner and more fluent syntax. While this new structure streamlines your application setup, it's easy to miss critical configurations—like applying the web
middleware group to your custom route files. In this post, we’ll explore how to properly register route files with necessary middleware and how to create your own custom middleware group based on web
in Laravel 11.
The Error: Undefined Variable $errors
One common issue developers face is the "undefined variable $errors" error in Laravel Blade views. This typically occurs when the web
middleware group is not applied to the route, which means features like session and validation errors aren't available in the view.
This issue often arises when using a custom route file—like admin.php
—without registering it properly under the web
middleware group.
Laravel 11 Style Routing
Laravel 11 introduces a new approach to route and middleware registration via the bootstrap/app.php
file using ->withRouting()
and ->withMiddleware()
. Here's how you can register your custom route file with the web
middleware:
use Illuminate\Support\Facades\Route;
use App\Http\Middleware\Authenticate;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
then: function () {
Route::middleware('web')
->prefix('admin')
->name('admin.')
->group(base_path('routes/admin.php'));
},
)
->create();
This ensures that all routes in admin.php
are wrapped in the web
middleware group, resolving issues like the undefined $errors
variable in Blade views.
Creating a Custom Middleware Group Based on web
If you want to create your own middleware group that inherits everything from web
but adds custom middleware, Laravel 11 makes it easy using the ->withMiddleware()
method:
use Illuminate\Foundation\Configuration\Middleware;
use App\Http\Middleware\LogRequests;
return Application::configure(basePath: dirname(__DIR__))
->withMiddleware(function (Middleware $middleware) {
$middleware->appendToGroup('custom-web', [
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
LogRequests::class, // Your custom middleware
]);
})
->withRouting(
web: __DIR__.'/../routes/web.php',
then: function () {
Route::middleware('custom-web')
->prefix('admin')
->group(base_path('routes/admin.php'));
},
)
->create();
Now, routes using custom-web
have everything web
provides, plus your additional logic.
Tips for Avoiding Middleware Issues
- Always double-check that your routes use the
web
or a custom middleware group that includes necessary features like session and error sharing.
- If you're using multiple route files, register them clearly inside the
then
callback of withRouting()
.
- Use descriptive names when defining custom middleware groups for clarity and maintainability.
Conclusion
Laravel 11’s modern route and middleware setup improves clarity and flexibility—but only if configured correctly. Missing the web
middleware group can break session-based features and validation. By following this guide, you can confidently create new route files and custom middleware groups without running into unexpected issues. Make sure you use the new Laravel 11 style and you’ll have a smooth and scalable project structure from the start.
Happy coding!