diff --git a/core/subresources.md b/core/subresources.md index 5c9e571337a..b06443cc802 100644 --- a/core/subresources.md +++ b/core/subresources.md @@ -5,8 +5,8 @@ In API Platform you can declare as many `ApiResource` as you want on a PHP class creating Subresources. Subresources work well by implementing your own state [providers](./state-providers.md) -or [processors](./state-processors.md). In API Platform we provide a working Doctrine layer for -subresources providing you add the correct configuration for URI Variables. +or [processors](./state-processors.md). In API Platform, we provide functional Doctrine and Eloquent layers for +subresources, as long as the correct configuration for URI variables is added. ## URI Variables Configuration @@ -14,10 +14,13 @@ URI Variables are configured via the `uriVariables` node on an `ApiResource`. It 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. -If you're using the Doctrine implementation, queries are automatically built using the provided links. +If you're using the Doctrine or the Eloquent implementation, queries are automatically built using the provided links. ### Answer to a Question +> [!NOTE] +> In Symfony we use the term “entities”, while the following documentation is mostly for Laravel “models”. + For this example we have two classes, a Question and an Answer. We want to find the Answer to the Question about the Universe using the following URI: `/question/42/answer`. @@ -84,6 +87,7 @@ class Question ``` ```yaml +# The YAML syntax is only supported for Symfony # api/config/api_platform/resources.yaml resources: App\Entity\Answer: ~ @@ -92,6 +96,7 @@ resources: ```xml + @@ -228,6 +237,9 @@ resources: ### Company Employee's +> [!NOTE] +> In Symfony we use the term “entities”, while the following documentation is mostly for Laravel “models”. + 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. 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 } ``` -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`. +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`. As a general rule, if the property we want to create a link from is in the `fromClass`, use `fromProperty`, if not, use `toProperty`. @@ -335,6 +347,9 @@ class Company { ## Security +> [!WARNING] +> This is not yet available with Laravel, you're welcome to contribute [on Github](github.com/api-platform/core) + In order to use Symfony's built-in security system on subresources the security option of the `Link` attribute can be used. 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.