Skip to content

Commit 8441373

Browse files
[12.x] Add section on assigning middleware to resource routes (#10553)
* Add section on assigning middleware to resource routes * Update note formatting for middleware order in controllers documentation * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 12d93a6 commit 8441373

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

controllers.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- [Localizing Resource URIs](#restful-localizing-resource-uris)
1515
- [Supplementing Resource Controllers](#restful-supplementing-resource-controllers)
1616
- [Singleton Resource Controllers](#singleton-resource-controllers)
17+
- [Middleware and Resource Controllers](#middleware-and-resource-controllers)
1718
- [Dependency Injection and Controllers](#dependency-injection-and-controllers)
1819

1920
<a name="introduction"></a>
@@ -538,6 +539,64 @@ Of course, API singleton resources may also be `creatable`, which will register
538539
```php
539540
Route::apiSingleton('photos.thumbnail', ProfileController::class)->creatable();
540541
```
542+
<a name="middleware-and-resource-controllers"></a>
543+
### Middleware and Resource Controllers
544+
545+
Laravel allows you to assign middleware to all, or only specific, methods of resource routes using the `middleware`, `middlewareFor`, and `withoutMiddlewareFor` methods. These methods provide fine-grained control over which middleware is applied to each resource action.
546+
547+
#### Applying Middleware to all Methods
548+
549+
You may use the `middleware` method to assign middleware to all routes generated by a resource or singleton resource route:
550+
551+
```php
552+
Route::resource('users', UserController::class)
553+
->middleware(['auth', 'verified']);
554+
555+
Route::singleton('profile', ProfileController::class)
556+
->middleware('auth');
557+
```
558+
559+
#### Applying Middleware to Specific Methods
560+
561+
You may use the `middlewareFor` method to assign middleware to one or more specific methods of a given resource controller:
562+
563+
```php
564+
Route::resource('users', UserController::class)
565+
->middlewareFor('show', 'auth');
566+
567+
Route::apiResource('users', UserController::class)
568+
->middlewareFor(['show', 'update'], 'auth');
569+
570+
Route::resource('users', UserController::class)
571+
->middlewareFor('show', 'auth')
572+
->middlewareFor('update', 'auth');
573+
574+
Route::apiResource('users', UserController::class)
575+
->middlewareFor(['show', 'update'], ['auth', 'verified']);
576+
```
577+
578+
The `middlewareFor` method may also be used in conjunction with singleton and API singleton resource controllers:
579+
580+
```php
581+
Route::singleton('profile', ProfileController::class)
582+
->middlewareFor('show', 'auth');
583+
584+
Route::apiSingleton('profile', ProfileController::class)
585+
->middlewareFor(['show', 'update'], 'auth');
586+
```
587+
588+
#### Excluding Middleware from Specific Methods
589+
590+
You may use the `withoutMiddlewareFor` method to exclude middleware from specific methods of a resource controller:
591+
592+
```php
593+
Route::middleware(['auth', 'verified', 'subscribed'])->group(function () {
594+
Route::resource('users', UserController::class)
595+
->withoutMiddlewareFor('index', ['auth', 'verified'])
596+
->withoutMiddlewareFor(['create', 'store'], 'verified')
597+
->withoutMiddlewareFor('destroy', 'subscribed');
598+
});
599+
```
541600

542601
<a name="dependency-injection-and-controllers"></a>
543602
## Dependency Injection and Controllers

0 commit comments

Comments
 (0)