In Laravel 11, you can chain both prefix() and middleware() methods before defining your route group. Here's how to achieve the same behavior as the previous version's array syntax:
Route::prefix('post')->middleware('auth')->group(function () {
Route::get('all', [Controller::class, 'post']);
Route::get('user', [Controller::class, 'post']);
});
For multiple middleware:
Route::middleware(['auth', 'admin'])
->prefix('admin')
->group(function () {
// Your routes here
});
You can also chain other route group features:
Route::prefix('api')
->middleware('api')
->domain('api.example.com')
->name('api.')
->group(function () {
// Your API routes
});
This fluent interface approach is the recommended way in Laravel 11 for grouping routes with shared configuration. The old array-based syntax has been deprecated in favor of this more readable method chaining syntax.