[英]How to create/define a sub resource via XML in API-Platform?
I am trying to create a sub resource via XML using Api Platform.我正在尝试使用 Api 平台通过 XML 创建子资源。
When I define the sub resource via a annotation on the entity, everything works as expected:当我通过实体上的注释定义子资源时,一切都按预期工作:
Entity/SocialProfile/SocialProfile.php实体/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:但是当我尝试通过.xml 定义此路由/端点时,如下所示:
Resources/config/api_resources/SocialProfile.xml资源/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 404 找不到路由
I have tested my SocialProfile.xml
file with a itemOperation
& everything is working as expected.我已经用itemOperation
测试了我的SocialProfile.xml
文件,一切都按预期工作。
I have updated my Resources/config/api_resources/SocialProfile.xml to look like the below, but I am still receiving a我已将 Resources/config/api_resources/SocialProfile.xml 更新为如下所示,但我仍然收到
404 route not found 404 找不到路由
<?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>
The configuration for the entity holding the subresource ( SocialProfile
, in this example).持有子资源的实体的配置(在本例中为SocialProfile
)。
<?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
.请注意,在您问题的第二个配置版本中,您删除了所有itemOperations
。 You should have at least the basic get
item operation so the library is able to build IRIs.您至少应该具有基本的get
项目操作,以便库能够构建 IRI。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.