Skip to content

Commit 8cc2864

Browse files
committed
docs: adds Laravel-specific doc parts
1 parent d4ac684 commit 8cc2864

File tree

1 file changed

+56
-6
lines changed

1 file changed

+56
-6
lines changed

core/subresources.md

Lines changed: 56 additions & 6 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
@@ -253,7 +265,8 @@ use Doctrine\ORM\Mapping as ORM;
253265
uriVariables: [
254266
'companyId' => new Link(fromClass: Company::class, toProperty: 'company'),
255267
'id' => new Link(fromClass: Employee::class),
256-
],
268+
],In Laravel
269+
257270
operations: [ new Get() ]
258271
)]
259272
#[ApiResource(
@@ -310,7 +323,7 @@ class Company
310323
}
311324
```
312325

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`.
326+
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`.
314327

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

@@ -335,12 +348,15 @@ class Company {
335348

336349
## Security
337350

338-
In order to use Symfony's built-in security system on subresources the security option of the `Link` attribute can be used.
351+
In order to use Symfony's or the Laravel built-in security system on subresources the security option of the `Link` attribute can be used.
352+
353+
### Symfony example for security
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.
341356

342357
Alternatively you can also use the `securityObjectName` to set a custom name.
343358

359+
344360
```php
345361
<?php
346362
#[ApiResource(
@@ -358,10 +374,44 @@ class Company {
358374
}
359375
```
360376

377+
### Laravel example for security
378+
379+
With Laravel, we can use the following code:
380+
381+
```php
382+
<?php
383+
#[ApiResource(
384+
uriTemplate: '/employees/{employeeId}/company',
385+
uriVariables: [
386+
'employeeId' => new Link(fromClass: Employee::class, toProperty: 'company', security: Gate::allows('some_voter', $company)),
387+
],
388+
operations: [
389+
new Get()
390+
]
391+
)]
392+
393+
class Company {
394+
// ...
395+
}
396+
```
397+
361398
This is currently an experimental feature disabled by default. To enable it please set `enable_link_security` to true:
362399

400+
### Symfony configuration to disable link security
401+
363402
```yaml
364403
# api/config/packages/api_platform.yaml
365404
api_platform:
366405
enable_link_security: true
367406
```
407+
408+
### Laravel configuration to disable link security
409+
410+
```php
411+
<?php
412+
// config/api-platform.php
413+
return [
414+
// ....
415+
'enable_link_security' => true,
416+
];
417+
```

0 commit comments

Comments
 (0)