Skip to content

Commit f126d81

Browse files
committed
docs: adds Laravel-specific doc parts
1 parent 6f225ca commit f126d81

File tree

1 file changed

+60
-7
lines changed

1 file changed

+60
-7
lines changed

core/subresources.md

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,23 @@ 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

13-
URI Variables are configured via the `uriVariables` node on an `ApiResource`. It's an array indexed by the variables present in your URI, `/companies/{companyId}/employees/{id}` has two uri variables `companyId` and `id`. 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.
13+
URI Variables are configured via the `uriVariables` node on an `ApiResource`. It's an array indexed by the variables
14+
present in your URI, `/companies/{companyId}/employees/{id}` has two uri variables `companyId` and `id`.
15+
For each of these, we need to create a `Link` between the previous and the next node, in this example the link between a
16+
Company and an Employee.
1417

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

1720
### Answer to a Question
1821

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

@@ -82,6 +88,7 @@ class Question
8288
```
8389

8490
```yaml
91+
# The YAML syntax is only supported for Symfony
8592
# api/config/api_platform/resources.yaml
8693
resources:
8794
App\Entity\Answer: ~
@@ -90,6 +97,7 @@ resources:
9097
9198
```xml
9299
<?xml version="1.0" encoding="UTF-8" ?>
100+
<!--The XML syntax is only supported for Symfony-->
93101
<!-- api/config/api_platform/resources.xml -->
94102

95103
<resources xmlns="https://api-platform.com/schema/metadata/resources-3.0"
@@ -138,6 +146,7 @@ class Answer
138146
```
139147

140148
```yaml
149+
# The YAML syntax is only supported for Symfony
141150
# api/config/api_platform/resources.yaml
142151
resources:
143152
App\Entity\Answer:
@@ -153,6 +162,7 @@ resources:
153162
```
154163
155164
```xml
165+
<!--The XML syntax is only supported for Symfony-->
156166
<resources xmlns="https://api-platform.com/schema/metadata/resources-3.0"
157167
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
158168
xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
@@ -197,6 +207,7 @@ If we had a `relatedQuestions` property on the `Answer` we could retrieve the co
197207
```
198208

199209
```yaml
210+
#The YAML syntax is only supported for Symfony
200211
# api/config/api_platform/resources.yaml
201212
resources:
202213
App\Entity\Question:
@@ -210,6 +221,7 @@ resources:
210221
```
211222
212223
```xml
224+
<!--The XML syntax is only supported for Symfony-->
213225
<resource class="App\Entity\Question" uriTemplate="/answers/{id}/related_questions.{_format}">
214226
<uriVariables>
215227
<uriVariable parameterName="id" fromClass="App\Entity\Answer" fromProperty="relatedQuestions"/>
@@ -226,6 +238,9 @@ resources:
226238

227239
### Company Employee's
228240

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

231246
The following declares a few subresources:
@@ -253,7 +268,8 @@ use Doctrine\ORM\Mapping as ORM;
253268
uriVariables: [
254269
'companyId' => new Link(fromClass: Company::class, toProperty: 'company'),
255270
'id' => new Link(fromClass: Employee::class),
256-
],
271+
],In Laravel
272+
257273
operations: [ new Get() ]
258274
)]
259275
#[ApiResource(
@@ -310,7 +326,7 @@ class Company
310326
}
311327
```
312328

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`.
329+
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`.
314330

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

@@ -335,12 +351,15 @@ class Company {
335351

336352
## Security
337353

338-
In order to use Symfony's built-in security system on subresources the security option of the `Link` attribute can be used.
354+
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.
355+
356+
### Symfony example for security
339357

340358
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.
341359

342360
Alternatively you can also use the `securityObjectName` to set a custom name.
343361

362+
344363
```php
345364
<?php
346365
#[ApiResource(
@@ -358,10 +377,44 @@ class Company {
358377
}
359378
```
360379

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

403+
### Symfony configuration to disable link security
404+
363405
```yaml
364406
# api/config/packages/api_platform.yaml
365407
api_platform:
366408
enable_link_security: true
367409
```
410+
411+
### Laravel configuration to disable link security
412+
413+
```php
414+
<?php
415+
// config/api-platform.php
416+
return [
417+
// ....
418+
'enable_link_security' => true,
419+
];
420+
```

0 commit comments

Comments
 (0)