PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 12, 2022

[FIXED] How to create/define a sub resource via XML in API-Platform?

 May 12, 2022     api-platform.com, symfony     No comments   

Issue

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>

Solution

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.



Answered By - yivi
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing