Skip to content

Commit 743247c

Browse files
committed
uri_template
1 parent e30797b commit 743247c

File tree

1 file changed

+39
-14
lines changed

1 file changed

+39
-14
lines changed

core/filters.md

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -323,40 +323,65 @@ class WithParameter
323323

324324
### `ReadLinkParameterProvider`
325325

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.
327328

328329
```php
329330
<?php
330331
// api/src/Resource/WithParameter.php
331332
use ApiPlatform\Metadata\ApiResource;
332333
use ApiPlatform\Metadata\Get;
334+
use ApiPlatform\Metadata\Link;
333335
use ApiPlatform\Metadata\Operation;
334336
use ApiPlatform\Metadata\QueryParameter;
335337
use ApiPlatform\State\ParameterProvider\ReadLinkParameterProvider;
336338
use App\Entity\Dummy;
337339

338-
#[ApiResource(operations: [
339-
new Get(
340-
uriTemplate: '/with_parameters_links',
341-
parameters: [
342-
'dummy' => new QueryParameter(
343-
provider: ReadLinkParameterProvider::class,
344-
extraProperties: ['resource_class' => Dummy::class]
345-
)
346-
],
347-
provider: [self::class, 'provideDummyFromParameter'],
348-
)
349-
])]
340+
#[Get(
341+
uriTemplate: 'with_parameters/{id}{._format}',
342+
uriVariables: [
343+
'id' => new Link(schema: ['type' => 'string', 'format' => 'uuid'], property: 'id'),
344+
],
345+
parameters: [
346+
'dummy' => new QueryParameter(
347+
provider: ReadLinkParameterProvider::class,
348+
extraProperties: [
349+
'resource_class' => Dummy::class,
350+
'uri_template' => '/dummies/{id}' // Optional: specify the template for the linked resource
351+
]
352+
)
353+
],
354+
provider: [self::class, 'provideDummyFromParameter'],
355+
)]
350356
class WithParameter
351357
{
352358
public static function provideDummyFromParameter(Operation $operation, array $uriVariables = [], array $context = []): object|array
353359
{
354-
// The value has been transformed from an identifier to an entity by the provider.
360+
// The dummy parameter has been resolved to the actual Dummy entity
361+
// based on the parameter value and the specified uri_template
355362
return $operation->getParameters()->get('dummy')->getValue();
356363
}
357364
}
358365
```
359366

367+
The provider will:
368+
- 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+
360385
### Creating a Custom Parameter Provider
361386

362387
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

Comments
 (0)