What are the Slack Archives?

It’s a history of our time together in the Slack Community! There’s a ton of knowledge in here, so feel free to search through the archives for a possible answer to your question.

Because this space is not active, you won’t be able to create a new post or comment here. If you have a question or want to start a discussion about something, head over to our categories and pick one to post in! You can always refer back to a post from Slack Archives if needed; just copy the link to use it as a reference..

Can someone please guide me how to add a sub-resource in Glue API? I have a resource with `

U048WDEP3R7
U048WDEP3R7 Posts: 217 πŸ§‘πŸ»β€πŸš€ - Cadet
edited November 2022 in Help

Can someone please guide me how to add a sub-resource in Glue API? I have a resource with [glue.de](http://glue.de).spryker.local/pickup and I would like to add a[glue.de](http://glue.de).spryker.local/pickup/add endpoint as well. Is it possible to do it in the same module? Following is the glimpse of my ResourceRoutePlugin.php file

...
class AddaxPickupPointsResourceRoutePlugin extends AbstractPlugin implements ResourceRoutePluginInterface
{
    public function configure(ResourceRouteCollectionInterface $resourceRouteCollection)
    : ResourceRouteCollectionInterface {

        // to get all pickup points
        $resourceRouteCollection->addGet('get', true);

        return $resourceRouteCollection;
    }
...

Comments

  • Alberto Reyer
    Alberto Reyer Posts: 690 πŸͺ - Explorer

    Sub Resources are possible through implementing \Spryker\Glue\GlueApplicationExtension\Dependency\Plugin\ResourceWithParentPluginInterface on the resource route plugin.

    But from a REST/JSON:API perspective there are a few issues here:

    1. resources should be plural (pickups instead of pickup)
  • Alberto Reyer
    Alberto Reyer Posts: 690 πŸͺ - Explorer
    edited November 2022
    1. Adding a resource of a type should be done via HTTP verbs: POST /pickups/ instead of POST /pickups/add
  • U048WDEP3R7
    U048WDEP3R7 Posts: 217 πŸ§‘πŸ»β€πŸš€ - Cadet

    Thank you @UL6DGRULR

  • U048WDEP3R7
    U048WDEP3R7 Posts: 217 πŸ§‘πŸ»β€πŸš€ - Cadet

    @UL6DGRULR Does that mean I have to:

    ..implements ResourceRoutePluginInterface, ResourceWithParentPluginInterface
    
  • Alberto Reyer
    Alberto Reyer Posts: 690 πŸͺ - Explorer
    edited November 2022
    use Spryker\Glue\CartCodesRestApi\CartCodesRestApiConfig;
    use Spryker\Glue\GlueApplicationExtension\Dependency\Plugin\ResourceRoutePluginInterface;
    
    class AddaxPickupPointsResourceRoutePlugin extends AbstractPlugin implements ResourceRoutePluginInterface, \Spryker\Glue\GlueApplicationExtension\Dependency\Plugin\ResourceWithParentPluginInterface
    {
        /**
         * {@inheritDoc}
         *
         * @api
         *
         * @return string
         */
        public function getParentResourceType(): string
        {
            return YourPickupPointsResourceClass::RESROUCE_PICKUP_POINT_OR_HOWEVER_YOU_NAMED_IT;
        }
    }
    
  • U048WDEP3R7
    U048WDEP3R7 Posts: 217 πŸ§‘πŸ»β€πŸš€ - Cadet

    @UL6DGRULR Thanks again. I am a beginner so please excuse me if my questions are too simple. AddaxPickupPointsResourceRoutePlugin is my main class which has the basic configurations for route collections. Do I have to add another class in the 'Plugin' directory that implements ResourceRoutePluginInterface, ResourceWithParentPluginInterface and eventually add it to GlueApplicationDependencyProvider::getResourceRoutePlugins() ?

  • Alberto Reyer
    Alberto Reyer Posts: 690 πŸͺ - Explorer

    Yes, for the main resource you add one resource class, for each subresource you add another class that implements the ResourceWithParentPluginInterface to hint spryker that it should prepend the parent resource to your subresource.

    /carts/{id} -> Main Resource (CartsResourceRoutePlugin)
    /carts/{id}/items -> Subresource (CartItemsResourceRoutePlugin that implements ResourceWithParentPluginInterface)

    You can also have a look on the implementations of the ResourceWithParentPluginInterface to get a better impression how they work

  • U048WDEP3R7
    U048WDEP3R7 Posts: 217 πŸ§‘πŸ»β€πŸš€ - Cadet

    @UL6DGRULR Thank you again for the explanation. Just one more question. Is it possible to add a subresesource like/carts/items instead of /carts/{id}/items ?

  • Alberto Reyer
    Alberto Reyer Posts: 690 πŸͺ - Explorer

    No, that's against the specification of JSON:API

  • U048WDEP3R7
    U048WDEP3R7 Posts: 217 πŸ§‘πŸ»β€πŸš€ - Cadet

    @UL6DGRULR Thank you for the explaination.