Reputation: 329
I am trying to create a sub resource via XML using Api Platform.
When I define the sub resource via a annotation on the entity, everything works as expected:
Entity/SocialProfile/SocialProfile.php
/**
* @ApiSubresource()
*
* @ORM\OneToMany(
* targetEntity="SoapSyliusSocialPlugin\Entity\Follow\Follow",
* mappedBy="follower",
* cascade={ "persist", "remove" }
* )
*/
protected $following;
Everything works as expected and I can then access the sub resource via the below path:
/api/v2/social-profiles/35471/followings
But when I try define this route/endpoint via .xml like the below:
Resources/config/api_resources/SocialProfile.xml
<?xml version="1.0" ?>
<resources xmlns="https://api-platform.com/schema/metadata"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://api-platform.com/schema/metadata https://api-platform.com/schema/metadata/metadata-2.0.xsd"
>
<resource class="SoapSyliusSocialPlugin\Entity\SocialProfile\SocialProfile" shortName="SocialProfile">
<attribute name="validation_groups">sylius</attribute>
<subresourceOperations>
<subresourceOperation name="api_social_profiles_followings_get_subresource">
<attribute name="method">GET</attribute>
</subresourceOperation>
</subresourceOperations>
<property name="following" writable="false" readable="true">
<subresource resourceClass="SoapSyliusSocialPlugin\Entity\Follow\Follow" />
</property>
</resource>
</resources>
I am getting a:
404 No route found
I have tested my SocialProfile.xml
file with a itemOperation
& everything is working as expected.
I have updated my Resources/config/api_resources/SocialProfile.xml to look like the below, but I am still receiving a
404 route not found
<?xml version="1.0" ?>
<resources xmlns="https://api-platform.com/schema/metadata"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://api-platform.com/schema/metadata https://api-platform.com/schema/metadata/metadata-2.0.xsd"
>
<resource class="SoapSyliusSocialPlugin\Entity\SocialProfile\SocialProfile" shortName="SocialProfile">
<attribute name="validation_groups">sylius</attribute>
<itemOperations></itemOperations>
<property name="following" writable="false" readable="true">
<subresource resourceClass="SoapSyliusSocialPlugin\Entity\Follow\Follow" collection="true"/>
</property>
</resource>
</resources>
Upvotes: 2
Views: 522
Reputation: 47297
The configuration for the entity holding the subresource (SocialProfile
, in this example).
<?xml version="1.0" ?>
<resources xmlns="https://api-platform.com/schema/metadata"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://api-platform.com/schema/metadata https://api-platform.com/schema/metadata/metadata-2.0.xsd"
>
<resource class="SoapSyliusSocialPlugin\Entity\SocialProfile\SocialProfile" shortName="SocialProfile">
<attribute name="validation_groups">sylius</attribute>
<property name="following" writable="false" readable="true">
<subresource resourceClass="SoapSyliusSocialPlugin\Entity\Follow\Follow" />
</property>
</resource>
</resources>
To configure things like normalization groups for the subresource, you do it in the other end of the relationship:
<?xml version="1.0" ?>
<resources xmlns="https://api-platform.com/schema/metadata"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://api-platform.com/schema/metadata https://api-platform.com/schema/metadata/metadata-2.0.xsd"
>
<resource class="SoapSyliusSocialPlugin\Entity\Follow\Follow" shortName="Follow">
<subresourceOperations>
<subresourceOperation name="api_social_profiles_followings_get_subresource">
<attribute name="method">GET</attribute>
</subresourceOperation>
</subresourceOperations>
</resource>
</resources>
Try with this. I have a few setup this way and working. If there is something wrong above should be because something does not match your class/resource names exactly, but you should be able to tweak that to fix it.
Note that in the second version of the configuration in your question you removed all itemOperations
. You should have at least the basic get
item operation so the library is able to build IRIs.
Upvotes: 1