Skip to content

Commit 48d9489

Browse files
authored
feat: add XML examples for subresources configuration (#1637)
1 parent 313666d commit 48d9489

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

core/subresources.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,20 @@ class Question
8181
}
8282
```
8383

84+
```xml
85+
<?xml version="1.0" encoding="UTF-8" ?>
86+
<!-- api/config/api_platform/resources.xml -->
87+
88+
<resources xmlns="https://api-platform.com/schema/metadata/resources-3.0"
89+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
90+
xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
91+
https://api-platform.com/schema/metadata/resources-3.0.xsd">
92+
<resource class="App\Entity\Question"/>
93+
<resource class="App\Entity\Answer"/>
94+
95+
</resources>
96+
```
97+
8498
[/codeSelector]
8599

86100
Now to create a new way of retrieving an Answer we will declare another resource on the `Answer` class.
@@ -115,6 +129,29 @@ class Answer
115129
}
116130
```
117131

132+
```xml
133+
<resources xmlns="https://api-platform.com/schema/metadata/resources-3.0"
134+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
135+
xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
136+
https://api-platform.com/schema/metadata/resources-3.0.xsd">
137+
138+
<resource class="App\Entity\Question"/>
139+
140+
<resource class="App\Entity\Answer"/>
141+
142+
<resource class="App\Entity\Answer" uriTemplate="/questions/{id}/answer">
143+
<uriVariables>
144+
<uriVariable parameterName="id" fromClass="App\Entity\Question" fromProperty="answer"/>
145+
</uriVariables>
146+
147+
<operations>
148+
<operation class="ApiPlatform\Metadata\Get"/>
149+
</operations>
150+
</resource>
151+
</resources>
152+
153+
```
154+
118155
[/codeSelector]
119156

120157
In this example, we instructed API Platform that the `Answer` we retrieve comes **from** the **class** `Question`
@@ -124,6 +161,8 @@ URI Variables are defined using Links (`ApiPlatform\Metadata\Link`). A `Link` ca
124161

125162
If we had a `relatedQuestions` property on the `Answer` we could retrieve the collection of related questions via the following definition:
126163

164+
[codeSelector]
165+
127166
```php
128167
#[ApiResource(
129168
uriTemplate: '/answers/{id}/related_questions.{_format}',
@@ -134,6 +173,21 @@ If we had a `relatedQuestions` property on the `Answer` we could retrieve the co
134173
)]
135174
```
136175

176+
```xml
177+
<resource class="App\Entity\Question" uriTemplate="/answers/{id}/related_questions.{_format}">
178+
<uriVariables>
179+
<uriVariable parameterName="id" fromClass="App\Entity\Answer" fromProperty="relatedQuestions"/>
180+
</uriVariables>
181+
182+
<operations>
183+
<operation class="ApiPlatform\Metadata\GetCollection"/>
184+
</operations>
185+
186+
</resource>
187+
```
188+
189+
[/codeSelector]
190+
137191
### Company Employee's
138192

139193
Note that in this example, we declared an association using Doctrine only between Employee and Company using a ManyToOne. There is no inverse association hence the use of `toProperty` in the URI Variables definition.

0 commit comments

Comments
 (0)