Skip to content

Commit 69c80c5

Browse files
committed
feat: add action middleware for resources and relationships
1 parent c103f90 commit 69c80c5

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

3.0/routing/README.md

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,20 @@ use the `middleware` method:
340340
$server->resource('posts')->middleware('my_middleware1', 'my_middleware2');
341341
```
342342

343-
:::tip
344-
If you want to add middleware to specific resource actions, you should
345-
use [Controller middleware.](https://laravel.com/docs/controllers#controller-middleware)
346-
:::
343+
Alternatively, you can specify middleware per resource action. To do that,
344+
provide an array to the `middleware()` method. Middleware that applies to
345+
every action should use the `"*"` key. Middleware for a specific action
346+
should be keyed by that action.
347+
348+
For example:
349+
350+
```php
351+
$server->resource('posts')->middleware([
352+
'*' => 'my_middleware1', // applies to all actions
353+
'show' => 'my_middleware2', // apples to just the "show" action
354+
'store' => ['my_middleware3', 'my_middleware4'], // use arrays for multiple
355+
]);
356+
```
347357

348358
## Defining Relationships
349359

@@ -449,6 +459,22 @@ The following example adds middleware to our `tags` relationship routes:
449459
$relationships->hasMany('tags')->middleware('my_middleware1', 'my_middleware2');
450460
```
451461

462+
Alternatively, you can specify middleware per relationship action. To do that,
463+
provide an array to the `middleware()` method. Middleware that applies to
464+
every relationship action should use the `"*"` key. Middleware for a specific
465+
action should be keyed by that action. Use our short-hands of `related`,
466+
`show`, `update`, `attach` and `detach` for the actions:
467+
468+
For example:
469+
470+
```php
471+
$relationships->hasMany('tags')->middleware([
472+
'*' => 'my_middleware1', // applies to all actions
473+
'show' => 'my_middleware2', // apples to just the "show" action
474+
'update' => ['my_middleware3', 'my_middleware4'], // use arrays for multiple
475+
]);
476+
```
477+
452478
## Route Model Binding
453479

454480
By default Laravel takes care of substituting parameter values for models using

0 commit comments

Comments
 (0)