1

I am trying to declare a subresource in my symfony app. I followed the api-platform doc about it: https://api-platform.com/docs/core/subresources/ The subresource does appear in the routes, but not under its parent resource.

The routes I currently have:

  api_files_get_collection                                         GET      ANY      ANY    /api/files.{_format}                                
  api_files_post_collection                                        POST     ANY      ANY    /api/files.{_format}                                
  api_files_get_item                                               GET      ANY      ANY    /api/files/{id}.{_format}                           
  api_files_patch_item                                             PATCH    ANY      ANY    /api/files/{id}.{_format}                           
  api_files_put_item                                               PUT      ANY      ANY    /api/files/{id}.{_format} 
  api_external_subscription_requests_get_collection                GET      ANY      ANY    /api/external_subscription_requests.{_format}       
  api_external_subscription_requests_post_publication_collection   POST     ANY      ANY    /api/subscribe                                      
  api_external_subscription_requests_get_item                      GET      ANY      ANY    /api/external_subscription_requests/{id}.{_format}

The routes I would like to have:

  api_files_get_collection                                         GET      ANY      ANY    /api/files.{_format}                                
  api_files_post_collection                                        POST     ANY      ANY    /api/files.{_format}                                
  api_files_get_item                                               GET      ANY      ANY    /api/files/{id}.{_format}                           
  api_files_patch_item                                             PATCH    ANY      ANY    /api/files/{id}.{_format}                           
  api_files_put_item                                               PUT      ANY      ANY    /api/files/{id}.{_format} 
  api_files_external_subscription_requests_get_collection                GET      ANY      ANY    /api/files/{id}/external_subscription_requests.{_format}       
  api_files_external_subscription_requests_post_publication_collection   POST     ANY      ANY    /api/files/{id}/subscribe                                      
  api_files_external_subscription_requests_get_item                      GET      ANY      ANY    /api/files/{id}/external_subscription_requests/{id}.{_format}

Code in App\Entity\File.php:

/**
 * @ORM\Entity
 * @ApiResource(
 *      attributes={"order"={"createdAt": "DESC"}},
 *      collectionOperations={
 *          "get"={
 *              "normalization_context"={"groups"={"model:timestampable", "file:collection:read"}},
 *          },
 *          "post",
 *      },
 *      itemOperations={
 *          "get"={
 *              "normalization_context"={"groups"={"model:timestampable", "file:item:read"}},
 *          },
 *          "patch",
 *          "put"
 *      },
 * )
 */
class File
{
    // ...

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\ExternalSubscriptionRequest", cascade={"all"}, mappedBy="file")
     * @Groups({
     *     "file:collection:read",
     *     "file:item:read"
     * })
     * @ApiSubresource()
     */
    private $external_subscription_requests;

    // ...
}

Code in App\Entity\ExternalSubscriptionRequest.php:

/**
 * @ORM\Entity
 * @ApiResource(
 *     collectionOperations={
 *         "get",
 *         "post_publication"={
 *             "method"="POST",
 *                 "path"="/subscribe",
 *                 "controller"=SubscribeToConso::class,
 *             }
 *     },
 *     itemOperations={
 *         "get",
 *     },
 * )
 */
class ExternalSubscriptionRequest
{
    // ...

    /**
     * @var File the file this request was made for
     *
     * @ORM\ManyToOne(targetEntity="App\Entity\File", inversedBy="external_subscription_requests")
     * @ORM\JoinColumn(referencedColumnName="id", nullable=false)
     *
     * @Groups({
     *     "external_subscription_request:collection:read",
     *     "external_subscription_request:item:read"
     * })
     */
    public $file;

    // ...

}
Tom
  • 1,357
  • 2
  • 13
  • 32
  • You may be missing [this part](https://api-platform.com/docs/core/operations/#expose-a-model-without-any-routes). – msg Oct 13 '20 at 19:17

0 Answers0