|
14 | 14 | - [Localizing Resource URIs](#restful-localizing-resource-uris)
|
15 | 15 | - [Supplementing Resource Controllers](#restful-supplementing-resource-controllers)
|
16 | 16 | - [Singleton Resource Controllers](#singleton-resource-controllers)
|
| 17 | + - [Middleware and Resource Controllers](#middleware-and-resource-controllers) |
17 | 18 | - [Dependency Injection and Controllers](#dependency-injection-and-controllers)
|
18 | 19 |
|
19 | 20 | <a name="introduction"></a>
|
@@ -538,6 +539,64 @@ Of course, API singleton resources may also be `creatable`, which will register
|
538 | 539 | ```php
|
539 | 540 | Route::apiSingleton('photos.thumbnail', ProfileController::class)->creatable();
|
540 | 541 | ```
|
| 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 | +``` |
541 | 600 |
|
542 | 601 | <a name="dependency-injection-and-controllers"></a>
|
543 | 602 | ## Dependency Injection and Controllers
|
|
0 commit comments