Skip to content

Commit 1a856de

Browse files
Merge pull request #2034 from vinceAmstoutz/docs-update-subresources-for-laravel
Subresources doc updated to include Laravel-specific parts
2 parents c26fb86 + e4b6ed0 commit 1a856de

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

core/subresources.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@ In API Platform you can declare as many `ApiResource` as you want on a PHP class
55
creating Subresources.
66

77
Subresources work well by implementing your own state [providers](./state-providers.md)
8-
or [processors](./state-processors.md). In API Platform we provide a working Doctrine layer for
9-
subresources providing you add the correct configuration for URI Variables.
8+
or [processors](./state-processors.md). In API Platform, we provide functional Doctrine and Eloquent layers for
9+
subresources, as long as the correct configuration for URI variables is added.
1010

1111
## URI Variables Configuration
1212

1313
URI Variables are configured via the `uriVariables` node on an `ApiResource`. It's an array indexed by the variables
1414
present in your URI, `/companies/{companyId}/employees/{id}` has two URI variables `companyId` and `id`.
1515
For each of these, we need to create a `Link` between the previous and the next node, in this example the link between a Company and an Employee.
1616

17-
If you're using the Doctrine implementation, queries are automatically built using the provided links.
17+
If you're using the Doctrine or the Eloquent implementation, queries are automatically built using the provided links.
1818

1919
### Answer to a Question
2020

21+
> [!NOTE]
22+
> In Symfony we use the term “entities”, while the following documentation is mostly for Laravel “models”.
23+
2124
For this example we have two classes, a Question and an Answer. We want to find the Answer to
2225
the Question about the Universe using the following URI: `/question/42/answer`.
2326

@@ -84,6 +87,7 @@ class Question
8487
```
8588

8689
```yaml
90+
# The YAML syntax is only supported for Symfony
8791
# api/config/api_platform/resources.yaml
8892
resources:
8993
App\Entity\Answer: ~
@@ -92,6 +96,7 @@ resources:
9296
9397
```xml
9498
<?xml version="1.0" encoding="UTF-8" ?>
99+
<!--The XML syntax is only supported for Symfony-->
95100
<!-- api/config/api_platform/resources.xml -->
96101

97102
<resources xmlns="https://api-platform.com/schema/metadata/resources-3.0"
@@ -140,6 +145,7 @@ class Answer
140145
```
141146

142147
```yaml
148+
# The YAML syntax is only supported for Symfony
143149
# api/config/api_platform/resources.yaml
144150
resources:
145151
App\Entity\Answer:
@@ -155,6 +161,7 @@ resources:
155161
```
156162
157163
```xml
164+
<!--The XML syntax is only supported for Symfony-->
158165
<resources xmlns="https://api-platform.com/schema/metadata/resources-3.0"
159166
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
160167
xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
@@ -199,6 +206,7 @@ If we had a `relatedQuestions` property on the `Answer` we could retrieve the co
199206
```
200207

201208
```yaml
209+
#The YAML syntax is only supported for Symfony
202210
# api/config/api_platform/resources.yaml
203211
resources:
204212
App\Entity\Question:
@@ -212,6 +220,7 @@ resources:
212220
```
213221
214222
```xml
223+
<!--The XML syntax is only supported for Symfony-->
215224
<resource class="App\Entity\Question" uriTemplate="/answers/{id}/related_questions.{_format}">
216225
<uriVariables>
217226
<uriVariable parameterName="id" fromClass="App\Entity\Answer" fromProperty="relatedQuestions"/>
@@ -228,6 +237,9 @@ resources:
228237

229238
### Company Employee's
230239

240+
> [!NOTE]
241+
> In Symfony we use the term “entities”, while the following documentation is mostly for Laravel “models”.
242+
231243
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.
232244

233245
The following declares a few subresources: - `/companies/{companyId}/employees/{id}` - get an employee belonging to a company - `/companies/{companyId}/employees` - get the company employee's
@@ -310,7 +322,7 @@ class Company
310322
}
311323
```
312324

313-
We did not define any Doctrine annotation here and if we want things to work properly with GraphQL, we need to map the `employees` field as a Link to the class `Employee` using the property `company`.
325+
We did not define any Doctrine or Eloquent annotation here and if we want things to work properly with GraphQL, we need to map the `employees` field as a Link to the class `Employee` using the property `company`.
314326

315327
As a general rule, if the property we want to create a link from is in the `fromClass`, use `fromProperty`, if not, use `toProperty`.
316328

@@ -335,6 +347,9 @@ class Company {
335347

336348
## Security
337349

350+
> [!WARNING]
351+
> This is not yet available with Laravel, you're welcome to contribute [on Github](github.com/api-platform/core)
352+
338353
In order to use Symfony's built-in security system on subresources the security option of the `Link` attribute can be used.
339354

340355
To restrict the access to a subresource based on the parent object simply use the Symfony expression language as you would do normally, with the exception that the name defined in `toProperty` or `fromProperty` is used to access the object.

0 commit comments

Comments
 (0)