Skip to content

Commit 471a93d

Browse files
add: [DOC] Custom Subresources Data Provider examples (#1454)
* add: Custom Subresources Data Provider examples I added exemple to help user to custom the subreshttps://github.com/api-platform/core/issues/2321#issuecomment-437357871 It would be nice to set a trace in the official doc. * changes for the review * add the listing top SubresourceDataProviderInterface * Update core/data-providers.md * Update core/data-providers.md * Update core/data-providers.md * Update core/data-providers.md * Update core/data-providers.md * Update core/data-providers.md Co-authored-by: Vincent <[email protected]>
1 parent 970a071 commit 471a93d

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

core/data-providers.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ For a given resource, you can implement two kinds of interface:
1717
is used when fetching a collection.
1818
* the [`ItemDataProviderInterface`](https://github.com/api-platform/core/blob/2.6/src/DataProvider/ItemDataProviderInterface.php)
1919
is used when fetching items.
20+
* the [`SubresourceDataProviderInterface`](https://github.com/api-platform/core/blob/2.6/src/DataProvider/SubresourceDataProviderInterface.php)
21+
is used when fetching items.
2022

2123
Both implementations can also implement a third, optional, interface called
2224
['RestrictedDataProviderInterface'](https://github.com/api-platform/core/blob/2.6/src/DataProvider/RestrictedDataProviderInterface.php)
@@ -126,6 +128,49 @@ services:
126128

127129
You can find a full working example in the [API Platform's demo application](https://github.com/api-platform/demo/blob/main/api/src/DataProvider/TopBookItemDataProvider.php).
128130

131+
## Custom Subresources Data Provider
132+
133+
You can add custom logic or update subresources data provider with the SubresourceDataProviderInterface .
134+
135+
```php
136+
<?php
137+
// api/src/DataProvider/BlogPostCollectionDataProvider.php
138+
139+
namespace App\DataProvider;
140+
141+
use ApiPlatform\Core\DataProvider\SubresourceDataProviderInterface;
142+
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
143+
use App\Entity\BlogPost;
144+
145+
final class BlogPostSubresourceDataProvider implements SubresourceDataProviderInterface, RestrictedDataProviderInterface
146+
{
147+
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
148+
{
149+
return BlogPost::class === $resourceClass;
150+
}
151+
152+
public function getSubresource(string $resourceClass, array $identifiers, array $context, string $operationName = null): iterable
153+
{
154+
// Retrieve the blog post collection from somewhere
155+
$blogPosts = $this->subresourceDataProvider->getSubresource($resourceClass, $identifiers, $context, $operationName);
156+
// write your own logic
157+
158+
return blogPosts;
159+
}
160+
}
161+
```
162+
163+
Declare the service in your services configuration:
164+
165+
```yaml
166+
# api/config/services.yaml
167+
services:
168+
# ...
169+
'App\DataProvider\BlogPostSubresourceDataProvider':
170+
arguments:
171+
- '@api_platform.doctrine.orm.default.subresource_data_provider'
172+
```
173+
129174
## Injecting the Serializer in an `ItemDataProvider`
130175

131176
In some cases, you may need to inject the `Serializer` in your `DataProvider`. There are no issues with the

0 commit comments

Comments
 (0)