Skip to content

Commit c25162b

Browse files
authored
Merge pull request #351 from Nightbr/patch-1
add maxDepth on subresources doc
2 parents f8b0a1a + 12c977f commit c25162b

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

core/operations.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,51 @@ Note that the operation name, here `api_questions_answer_get_subresource`, is th
344344
It'll be automatically set to `$resources_$subresource(s)_get_subresource`. To find the correct operation name you
345345
may use `bin/console debug:router`.
346346

347+
### Control the depth of subresources
348+
349+
You can control depth of subresources with the parameter `maxDepth`. For example, if `Answer` entity also have subresource such as `comments`and you don't want the route `api/questions/{id}/answers/{id}/comments` to be generated. You can do this by adding the parameter maxDepth in ApiSubresource annotation or yml/xml file configuration.
350+
351+
```php
352+
<?php
353+
// src/AppBundle/Entity/Question.php
354+
355+
use ApiPlatform\Core\Annotation\ApiProperty;
356+
use ApiPlatform\Core\Annotation\ApiResource;
357+
use ApiPlatform\Core\Annotation\ApiSubresource;
358+
use Doctrine\ORM\Mapping as ORM;
359+
360+
/**
361+
* @ORM\Entity
362+
* @ApiResource
363+
*/
364+
class Question
365+
{
366+
/**
367+
* @ORM\Column(type="integer")
368+
* @ORM\Id
369+
* @ORM\GeneratedValue(strategy="AUTO")
370+
*/
371+
private $id;
372+
373+
/**
374+
* @ORM\Column
375+
*/
376+
public $content;
377+
378+
/**
379+
* @ORM\OneToOne(targetEntity="Answer", inversedBy="question")
380+
* @ORM\JoinColumn(referencedColumnName="id", unique=true)
381+
* @ApiSubresource(maxDepth=1)
382+
*/
383+
public $answer;
384+
385+
public function getId()
386+
{
387+
return $this->id;
388+
}
389+
}
390+
```
391+
347392
## Creating Custom Operations and Controllers
348393

349394
API Platform can leverage the Symfony routing system to register custom operation related to custom controllers. Such custom

0 commit comments

Comments
 (0)