Skip to content

Commit 7f48f50

Browse files
authored
feat(controllers): using the PlaceholderAction (#1655)
1 parent 59896db commit 7f48f50

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

core/controllers.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,74 @@ App\Entity\Book:
151151
It is mandatory to set the `method`, `path` and `controller` attributes. They allow API Platform to configure the routing path and
152152
the associated controller respectively.
153153

154+
## Using the PlaceholderAction
155+
156+
Complex use cases may lead you to create multiple custom operations.
157+
158+
In such a case, you will probably create the same amount of custom controllers while you may not need to perform custom logic inside.
159+
160+
To avoid that, API Platform provides the `ApiPlatform\Action\PlaceholderAction` which behaves the same when using the [built-in operations](operations.md#operations).
161+
162+
You just need to set the `controller` attribute with this class. Here, the previous example updated:
163+
164+
[codeSelector]
165+
166+
```php
167+
// api/src/Entity/Book.php
168+
namespace App\Entity;
169+
170+
use ApiPlatform\Action\PlaceholderAction;
171+
use ApiPlatform\Metadata\ApiResource;
172+
use ApiPlatform\Metadata\Get;
173+
use ApiPlatform\Metadata\Post;
174+
175+
#[ApiResource(operations: [
176+
new Get(),
177+
new Post(
178+
name: 'publication',
179+
uriTemplate: '/books/{id}/publication',
180+
controller: PlaceholderAction::class
181+
)
182+
])]
183+
class Book
184+
{
185+
// ...
186+
}
187+
```
188+
189+
```yaml
190+
# api/config/api_platform/resources.yaml
191+
App\Entity\Book:
192+
operations:
193+
ApiPlatform\Metadata\Get: ~
194+
post_publication:
195+
class: ApiPlatform\Metadata\Post
196+
method: POST
197+
uriTemplate: /books/{id}/publication
198+
controller: ApiPlatform\Action\PlaceholderAction
199+
```
200+
201+
```xml
202+
<?xml version="1.0" encoding="UTF-8" ?>
203+
<!-- api/config/api_platform/resources.xml -->
204+
205+
<resources
206+
xmlns="https://api-platform.com/schema/metadata/resources-3.0"
207+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
208+
xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
209+
https://api-platform.com/schema/metadata/resources-3.0.xsd">
210+
<resource class="App\Entity\Book">
211+
<operations>
212+
<operation class="ApiPlatform\Metadata\Get" />
213+
<operation class="ApiPlatform\Metadata\Post" name="post_publication" uriTemplate="/books/{id}/publication"
214+
controller="ApiPlatform\Action\PlaceholderAction" />
215+
</operations>
216+
</resource>
217+
</resources>
218+
```
219+
220+
[/codeSelector]
221+
154222
## Using Serialization Groups
155223

156224
You may want different serialization groups for your custom operations. Just configure the proper `normalizationContext` and/or `denormalizationContext` in your operation:

0 commit comments

Comments
 (0)