Skip to content

Subresource operations nomenclature update #1084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions core/subresources.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ use ApiPlatform\Core\Annotation\ApiResource;

/**
* @ApiResource(subresourceOperations={
* "api_questions_answer_get_subresource"={
* "answer_get"={
* "method"="GET",
* "normalization_context"={"groups"={"foobar"}}
* }
Expand All @@ -158,7 +158,7 @@ Or using YAML:
# api/config/api_platform/resources.yaml
App\Entity\Answer:
subresourceOperations:
api_questions_answer_get_subresource:
answer_get:
method: 'GET'
normalization_context: {groups: ['foobar']}
```
Expand All @@ -175,7 +175,7 @@ Or in XML:
https://api-platform.com/schema/metadata/metadata-2.0.xsd">
<resource class="App\Entity\Answer">
<subresourceOperations>
<subresourceOperation name="api_questions_answer_get_subresource">
<subresourceOperation name="answer_get">
<attribute name="method">GET</attribute>
<attribute name="normalization_context">
<attribute name="groups">
Expand All @@ -191,9 +191,12 @@ Or in XML:
In the previous examples, the `method` attribute is mandatory, because the operation name doesn't match a supported HTTP
method.

Note that the operation name, here `api_questions_answer_get_subresource`, is the important keyword.
It'll be automatically set to `$resources_$subresource(s)_get_subresource`. To find the correct operation name you
may use `bin/console debug:router`.
Note that the operation name, here `answer_get`, is the important keyword.
It's a shortcut name for the operation id: `api_$resources_$subresource(s)_get_subresource`.
If you want to customize a nested operation (`question/{id}/answer/question`), the operation name will match the nesting order (`answer_question_get`)

You may find the correct operation name by using `bin/console debug:router`,
then remove the prefix `api_$resources` and the suffix `_subresource`.

## Using Custom Paths

Expand All @@ -207,7 +210,7 @@ You can control the path of subresources with the `path` option of the `subresou
* ...
* @ApiResource(
* subresourceOperations={
* "api_questions_answer_get_subresource"={
* "answer_get"={
* "method"="GET",
* "path"="/questions/{id}/all-answers"
* },
Expand All @@ -231,7 +234,7 @@ The `subresourceOperations` attribute also allows you to add an access control o
* ...
* @ApiResource(
* subresourceOperations={
* "api_questions_answer_get_subresource"= {
* "answer_get"= {
* "security"="has_role('ROLE_AUTHENTICATED')"
* }
* }
Expand Down