Skip to content

Commit 2ff73af

Browse files
greg0iremeyerbaptiste
authored andcommitted
Document requirements (#312)
1 parent d8ca8c6 commit 2ff73af

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

core/operations.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ just by specifying the enabled HTTP method.
115115
The URL, the HTTP method and the Hydra context passed to documentation generators of operations is easy to configure.
116116

117117
In the next example, both `GET` and `PUT` operations are registered with custom URLs. Those will override the default generated
118-
URLs. In addition to that, we replace the Hydra context for the `PUT` operation.
118+
URLs. In addition to that, we replace the Hydra context for the `PUT` operation, and require the `id` parameter to be an integer.
119119

120120
<configurations>
121121

@@ -128,8 +128,8 @@ use ApiPlatform\Core\Annotation\ApiResource;
128128

129129
/**
130130
* @ApiResource(itemOperations={
131-
* "get"={"method"="GET", "path"="/grimoire/{id}"},
132-
* "put"={"method"="PUT", "path"="/grimoire/{id}/update", "hydra_context"={"foo"="bar"},
131+
* "get"={"method"="GET", "path"="/grimoire/{id}", "requirements"={"id"="\d+"}},
132+
* "put"={"method"="PUT", "path"="/grimoire/{id}/update", "hydra_context"={"foo"="bar"}},
133133
* })
134134
*/
135135
class Book
@@ -146,10 +146,14 @@ AppBundle\Entity\Book:
146146
get:
147147
method: 'GET'
148148
path: '/grimoire/{id}'
149+
requirements:
150+
id: '\d+'
149151
put:
150152
method: 'PUT'
151153
path: '/grimoire/{id}/update'
152154
hydra_context: { foo: 'bar' }
155+
requirements:
156+
id: '\d+'
153157
```
154158
155159
```xml
@@ -165,13 +169,19 @@ AppBundle\Entity\Book:
165169
<itemOperation name="get">
166170
<attribute name="method">GET</attribute>
167171
<attribute name="path">/grimoire/{id}</attribute>
172+
<attribute name="requirements">
173+
<attribute name="id">\d+</attribute>
174+
</attribute>
168175
</itemOperation>
169176
<itemOperation name="put">
170177
<attribute name="method">PUT</attribute>
171178
<attribute name="path">/grimoire/{id}/update</attribute>
172179
<attribute name="hydra_context">
173180
<attribute name="foo">bar</attribute>
174181
</attribute>
182+
<attribute name="requirements">
183+
<attribute name="id">\d+</attribute>
184+
</attribute>
175185
</itemOperation>
176186
</itemOperations>
177187
</resource>
@@ -486,7 +496,7 @@ In this case, the entity will pass through [all built-in event listeners](events
486496
automatically validated, persisted and serialized in JSON-LD. Then the Symfony kernel will send the resulting document to
487497
the client.
488498

489-
Alternatively, you can also use standard Symfony controller and YAML or XML route declarations. The following example do
499+
Alternatively, you can also use standard Symfony controller and YAML or XML route declarations. The following example does
490500
exactly the same thing than the previous example in a more Symfony-like fashion:
491501

492502
```php

0 commit comments

Comments
 (0)