Create Glue API resources with parent-child relationships

Edit on GitHub

Glue API lets you create resources with parent-child relationships or, in other words, nested resources. To enable such relationship, you need to create a resource that implements Spryker\Glue\GlueApplicationExtension\Dependency\Plugin\ResourceWithParentPluginInterface.

Such a plugin routes requests from parent resources to the correct child resource. This interface must be implemented together with ResourceInterface or with the convention resource interface.

The ResourceInterface interface provides only one method: getParentResourceType. The method must return the type of the immediate parent resource within the context in which the child resource is implemented.

Let’s say you have a module named ModuleRestApi, where you want to have a new endpoint /module/1/bar with GET and POST methods. To create the new endpoint, follow these steps:

  1. Create a resource using the steps described in the Create storefront resources or Create backend resources guide.
  2. Add a child resource name:

src\Pyz\Glue\ModuleRestApi\ModuleRestApiConfig.php

<?php

namespace Pyz\Glue\ModuleRestApi;

use Spryker\Glue\Kernel\AbstractBundleConfig;

class ModuleRestApiConfig extends AbstractBundleConfig
{
    public const RESOURCE_BAR = 'bar';
}
  1. Create BarResource that implements ResourceWithParentPluginInterface:
src\Pyz\Glue\ModuleRestApi\Plugin\BarResource.php
<?php

namespace Pyz\Glue\ModuleRestApi\Plugin;

use Generated\Shared\Transfer\GlueResourceMethodCollectionTransfer;
use Generated\Shared\Transfer\GlueResourceMethodConfigurationTransfer;
use Generated\Shared\Transfer\ModuleRestAttributesTransfer;
use Spryker\Glue\ModuleRestApi\Controller\ModuleResourceController;
use Spryker\Glue\ModuleRestApi\ModuleRestApiConfig;
use Spryker\Glue\GlueApplication\Plugin\GlueApplication\AbstractResourcePlugin;
use Spryker\Glue\GlueApplicationExtension\Dependency\Plugin\ResourceInterface;
use Spryker\Glue\GlueApplicationExtension\Dependency\Plugin\ResourceWithParentPluginInterface;

class ModuleRestResource extends AbstractResourcePlugin implements ResourceInterface, ResourceWithParentPluginInterface
{
    public function getType(): string
    {
        return ModuleRestApiConfig::RESOURCE_BAR;
    }

    public function getController(): string
    {
        return ModuleResourceController::class;
    }

    public function getDeclaredMethods(): GlueResourceMethodCollectionTransfer
    {
        return (new GlueResourceMethodCollectionTransfer())
            ->setGet(new GlueResourceMethodConfigurationTransfer())
            ->setPost(
                (new GlueResourceMethodConfigurationTransfer())
                    ->setAction('postAction')->setAttributes(ModuleRestAttributesTransfer::class),
            );
    }

    public function getParentResourceType(): string
    {
        return ModuleRestApiConfig::RESOURCE_MODULE;
    }
}
  1. Declare the resource:

\Pyz\Glue\GlueStorefrontApiApplication\GlueStorefrontApiApplicationDependencyProvider

<?php

namespace Pyz\Glue\GlueStorefrontApiApplication;

use Pyz\Glue\ModuleRestApi\Plugin\BarResource;
use Spryker\Glue\GlueStorefrontApiApplication\GlueStorefrontApiApplicationDependencyProvider as SprykerGlueStorefrontApiApplicationDependencyProvider;

class GlueStorefrontApiApplicationDependencyProvider extends SprykerGlueStorefrontApiApplicationDependencyProvider
{
    protected function getResourcePlugins(): array
    {
        return [
            new BarResource(),
            //Parent resource for BarResource
            new ModuleResource(),
        ];
    }
}
  1. Access https://glue-storefront.mysprykershop.com/module/1/bar.