You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: core/filters.md
+39-14Lines changed: 39 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -323,40 +323,65 @@ class WithParameter
323
323
324
324
### `ReadLinkParameterProvider`
325
325
326
-
This provider fetches a linked resource from a given identifier. This is useful when you need to load a related entity to use later, for example in your own state provider.
326
+
This provider fetches a linked resource from a given identifier. This is useful when you need to load a related entity to use later, for example in your own state provider.
327
+
When you have an API resource with a custom `uriTemplate` that includes parameters, the `ReadLinkParameterProvider` can automatically resolve the linked resource using the operation's URI template. This is particularly useful for nested resources or when you need to load a parent resource based on URI variables.
327
328
328
329
```php
329
330
<?php
330
331
// api/src/Resource/WithParameter.php
331
332
use ApiPlatform\Metadata\ApiResource;
332
333
use ApiPlatform\Metadata\Get;
334
+
use ApiPlatform\Metadata\Link;
333
335
use ApiPlatform\Metadata\Operation;
334
336
use ApiPlatform\Metadata\QueryParameter;
335
337
use ApiPlatform\State\ParameterProvider\ReadLinkParameterProvider;
- Take the parameter value (e.g., a UUID or identifier)
369
+
- Use the `resource_class` to determine which resource to load
370
+
- Optionally use the `uri_template` from `extraProperties` to construct the proper operation for loading the resource
371
+
- Return the loaded entity, making it available in your state provider
372
+
373
+
You can also control error handling by setting `throw_not_found` to `false` in the `extraProperties` to prevent exceptions when the linked resource is not found:
374
+
375
+
```php
376
+
'dummy' => new QueryParameter(
377
+
provider: ReadLinkParameterProvider::class,
378
+
extraProperties: [
379
+
'resource_class' => Dummy::class,
380
+
'throw_not_found' => false // Won't throw NotFoundHttpException if resource is missing
381
+
]
382
+
)
383
+
```
384
+
360
385
### Creating a Custom Parameter Provider
361
386
362
387
You can create your own providers to implement any custom logic. A provider must implement `ParameterProviderInterface`. The `provide` method can modify the parameter's value or even return a modified `Operation` to alter the request handling flow.
0 commit comments