Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Feature: add action middleware for resources and relationships #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions 3.0/routing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down