@@ -81,6 +81,20 @@ class Question
81
81
}
82
82
```
83
83
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
+
84
98
[ /codeSelector]
85
99
86
100
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
115
129
}
116
130
```
117
131
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
+
118
155
[ /codeSelector]
119
156
120
157
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
124
161
125
162
If we had a ` relatedQuestions ` property on the ` Answer ` we could retrieve the collection of related questions via the following definition:
126
163
164
+ [ codeSelector]
165
+
127
166
``` php
128
167
#[ApiResource(
129
168
uriTemplate: '/answers/{id}/related_questions.{_format}',
@@ -134,6 +173,21 @@ If we had a `relatedQuestions` property on the `Answer` we could retrieve the co
134
173
)]
135
174
```
136
175
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
+
137
191
### Company Employee's
138
192
139
193
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