From b969317c08e380daf3b3ad585ef9748f4a80b28a Mon Sep 17 00:00:00 2001 From: Christopher Gammie Date: Sat, 10 Feb 2024 14:15:00 +0000 Subject: [PATCH] feat: add action middleware for resources and relationships --- 3.0/routing/README.md | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/3.0/routing/README.md b/3.0/routing/README.md index 364511f..33dfc11 100644 --- a/3.0/routing/README.md +++ b/3.0/routing/README.md @@ -340,10 +340,20 @@ use the `middleware` method: $server->resource('posts')->middleware('my_middleware1', 'my_middleware2'); ``` -:::tip -If you want to add middleware to specific resource actions, you should -use [Controller middleware.](https://laravel.com/docs/controllers#controller-middleware) -::: +Alternatively, you can specify middleware per resource action. To do that, +provide an array to the `middleware()` method. Middleware that applies to +every action should use the `"*"` key. Middleware for a specific action +should be keyed by that action. + +For example: + +```php +$server->resource('posts')->middleware([ + '*' => 'my_middleware1', // applies to all actions + 'show' => 'my_middleware2', // apples to just the "show" action + 'store' => ['my_middleware3', 'my_middleware4'], // use arrays for multiple +]); +``` ## Defining Relationships @@ -449,6 +459,22 @@ The following example adds middleware to our `tags` relationship routes: $relationships->hasMany('tags')->middleware('my_middleware1', 'my_middleware2'); ``` +Alternatively, you can specify middleware per relationship action. To do that, +provide an array to the `middleware()` method. Middleware that applies to +every relationship action should use the `"*"` key. Middleware for a specific +action should be keyed by that action. Use our short-hands of `related`, +`show`, `update`, `attach` and `detach` for the actions: + +For example: + +```php +$relationships->hasMany('tags')->middleware([ + '*' => 'my_middleware1', // applies to all actions + 'show' => 'my_middleware2', // apples to just the "show" action + 'update' => ['my_middleware3', 'my_middleware4'], // use arrays for multiple +]); +``` + ## Route Model Binding By default Laravel takes care of substituting parameter values for models using