2

I have creating a extension for get cms static block content using rest api.

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Snowdog\CmsApi\Api\PageManagerInterface"
                type="Snowdog\CmsApi\Model\PageManager" />
    <preference for="Snowdog\CmsApi\Api\BlockManagerInterface"
                type="Snowdog\CmsApi\Model\BlockManager" />
    <preference for="Snowdog\CmsApi\Api\Data\BlockInterface"
                type="Snowdog\CmsApi\Model\Block" />
    <preference for="Snowdog\CmsApi\Api\Data\PageInterface"
                type="Snowdog\CmsApi\Model\Page" />
    <preference for="Snowdog\CmsApi\Api\Data\PageSearchResultsInterface"
                type="Magento\Framework\Api\SearchResults" />
    <preference for="Snowdog\CmsApi\Api\Data\BlockSearchResultsInterface"
                type="Magento\Framework\Api\SearchResults" />
    <type name="Snowdog\CmsApi\Model\BlockManager">
        <arguments>
            <argument name="collectionProcessor" xsi:type="object">Magento\Cms\Model\Api\SearchCriteria\BlockCollectionProcessor</argument>
        </arguments>
    </type>
    <type name="Snowdog\CmsApi\Model\PageManager">
        <arguments>
            <argument name="collectionProcessor" xsi:type="object">Magento\Cms\Model\Api\SearchCriteria\PageCollectionProcessor</argument>
        </arguments>
    </type>
</config>

webapi.xml

    <?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <!-- Cms Page -->
    <route url="/V1/snowdog/cmsPage/:pageId" method="GET">
        <service class="Snowdog\CmsApi\Api\PageManagerInterface" method="getById"/>
        <resources>
            <resource ref="Magento_Cms::page"/>
        </resources>
    </route>
    <route url="/V1/snowdog/cmsPageIdentifier/:identifier/storeId/:storeId" method="GET">
        <service class="Snowdog\CmsApi\Api\PageManagerInterface" method="getByIdentifier"/>
        <resources>
            <resource ref="Magento_Cms::page"/>
        </resources>
    </route>
    <route url="/V1/snowdog/cmsPage/search" method="GET">
        <service class="Snowdog\CmsApi\Api\PageManagerInterface" method="getList"/>
        <resources>
            <resource ref="Magento_Cms::page"/>
        </resources>
    </route>
    <!-- Cms Block -->
    <route url="/V1/snowdog/cmsBlock/:blockId" method="GET">
        <service class="Snowdog\CmsApi\Api\BlockManagerInterface" method="getById"/>
        <resources>
            <resource ref="Magento_Cms::block"/>
        </resources>
    </route>
    <route url="/V1/snowdog/cmsBlockIdentifier/:identifier/storeId/:storeId" method="GET">
        <service class="Snowdog\CmsApi\Api\BlockManagerInterface" method="getByIdentifier"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
    <route url="/V1/snowdog/cmsBlock/search" method="GET">
        <service class="Snowdog\CmsApi\Api\BlockManagerInterface" method="getList"/>
        <resources>
            <resource ref="Magento_Cms::block"/>
        </resources>
    </route>
</routes>
  1. passing url

http://vaibhav.local/rest/V1/snowdog/cmsBlock?29

  1. getting error

When I am hit URL I got Request does not match any route. issue.

Can anyone help me to solve this.

Thanks In advance..!

  • You should try with this rest/V1/snowdog/cmsBlock/:29 – Chirag Patel Apr 1 at 9:26
  • its not working bro – Sarvesh Tiwari Apr 1 at 9:28
  • can you share your di.xml code and BlockManagerInterface code? – Chirag Patel Apr 1 at 9:33
  • please check my updated question – Sarvesh Tiwari Apr 1 at 9:36
1

Your endpoint should be:

rest/V1/snowdog/cmsBlock/29

instead of

rest/V1/snowdog/V1/snowdog/cmsBlock?29

UPDATE: it should be routes not route.

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route url="/V1/snowdog/cmsBlock/:blockId" method="GET">
        <service class="Snowdog\CmsApi\Api\BlockManagerInterface" method="getById"/>
        <resources>
            <resource ref="Magento_Cms::block"/>
        </resources>
    </route>
</routes>
0

You are calling your web service as

rest/V1/snowdog/V1/snowdog/cmsBlock?29

You need to call it like

rest/V1/snowdog/cmsBlock?29

Thanks

0

Follow below step and verify your code.

Step 1 : Make sure you have enabled `Snowdog_CmsApi module.

Step 2 :

etc/di.xml

<preference for="Snowdog\CmsApi\Api\BlockManagerInterface"
            type="Snowdog\CmsApi\Model\BlockManager" />

Step 3 :

etc/webapi.xml

<!-- Cms Block -->
<route url="/V1/snowdog/cmsBlock/:blockId" method="GET">
    <service class="Snowdog\CmsApi\Api\BlockManagerInterface" method="getById"/>
    <resources>
        <resource ref="Magento_Cms::block"/>
    </resources>
</route>

Step 4 :

Api/BlockManagerInterface.php

<?php
 namespace Snowdog\CmsApi\Api;
 interface BlockManagerInterface
 {
/**
 * @param int $blockId
 * @return \Magento\Cms\Api\Data\BlockInterface
 */
public function getById($blockId);
/**
 * @param string $identifier
 * @param int $storeId
 * @return \Magento\Cms\Api\Data\BlockInterface
 * @throws \Magento\Framework\Exception\NoSuchEntityException
 */
public function getByIdentifier($identifier, $storeId = null);
/**
 * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
 * @return \Snowdog\CmsApi\Api\Data\BlockSearchResultsInterface
 * @throws \Magento\Framework\Exception\LocalizedException
 */
public function getList(\Magento\Framework\Api\SearchCriteriaInterface 
$searchCriteria);
}

Step 5

Model/BlockManager.php

<?php
 namespace Snowdog\CmsApi\Model;
 use Magento\Cms\Api\BlockRepositoryInterface;
 use Magento\Cms\Api\Data\BlockInterface;
 use Magento\Cms\Model\BlockFactory;
 use Magento\Cms\Model\ResourceModel\Block;
 use Magento\Cms\Model\Template\FilterProvider;
 use Magento\Framework\Api\DataObjectHelper;
 use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
 use Magento\Framework\Exception\NoSuchEntityException;
 use Magento\Store\Model\App\Emulation;
 use Snowdog\CmsApi\Api\BlockManagerInterface;
 use Snowdog\CmsApi\Api\Data\BlockInterfaceFactory;
 use Snowdog\CmsApi\Api\Data\BlockSearchResultsInterfaceFactory;
 use Magento\Framework\App\State;
 use Magento\Framework\App\Area;
 /**
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
 class BlockManager extends ManagerBase implements BlockManagerInterface
 {
/**
 * @var BlockRepositoryInterface
 */
private $blockRepository;
/**
 * @var FilterProvider
 */
private $filterProvider;
/**
 * @var BlockFactory
 */
private $blockFactory;
/**
 * @var Block
 */
private $blockResource;
/**
 * @var BlockSearchResultsInterfaceFactory
 */
private $searchResultsFactory;
/**
 * @var Block\CollectionFactory
 */
private $blockCollectionFactory;
/**
 * @var CollectionProcessorInterface
 */
private $collectionProcessor;
/**
 * @var BlockInterfaceFactory
 */
private $blockDtoFactory;
/**
 * @var DataObjectHelper
 */
private $dataObjectHelper;
/**
 * @var State
 */
private $appState;
/**
 * @var Emulation
 */
private $emulation;
/**
 * @param BlockRepositoryInterface $blockRepository
 * @param FilterProvider $filterProvider
 * @param BlockFactory $blockFactory
 * @param Block $blockResource
 * @param Block\CollectionFactory $blockCollectionFactory
 * @param BlockSearchResultsInterfaceFactory $searchResultsFactory
 * @param CollectionProcessorInterface $collectionProcessor
 * @param BlockInterfaceFactory $blockDtoFactory
 * @param DataObjectHelper $dataObjectHelper
 * @param State $appState
 * @param Emulation $emulation
 */
public function __construct(
    BlockRepositoryInterface $blockRepository,
    FilterProvider $filterProvider,
    BlockFactory $blockFactory,
    Block $blockResource,
    Block\CollectionFactory $blockCollectionFactory,
    BlockSearchResultsInterfaceFactory $searchResultsFactory,
    CollectionProcessorInterface $collectionProcessor,
    BlockInterfaceFactory $blockDtoFactory,
    DataObjectHelper $dataObjectHelper,
    State $appState,
    Emulation $emulation
) {
    $this->blockRepository = $blockRepository;
    $this->filterProvider = $filterProvider;
    $this->blockFactory = $blockFactory;
    $this->blockResource = $blockResource;
    $this->searchResultsFactory = $searchResultsFactory;
    $this->blockCollectionFactory = $blockCollectionFactory;
    $this->collectionProcessor = $collectionProcessor;
    $this->blockDtoFactory = $blockDtoFactory;
    $this->dataObjectHelper = $dataObjectHelper;
    $this->appState = $appState;
    $this->emulation = $emulation;
}
/**
 * @inheritdoc
 */
public function getById($blockId)
{
    $block = $this->blockRepository->getById($blockId);
    $content = $this->getBlockContentFiltered($block->getContent());
    $block->setContent($content);
    return $block;
}
/**
 * @inheritdoc
 */
public function getByIdentifier($identifier, $storeId = null)
{
    $block = $this->blockFactory->create();
    $block->setStoreId($storeId);
    $this->blockResource->load($block, $identifier, BlockInterface::IDENTIFIER);
    if (!$block->getId()) {
        throw new NoSuchEntityException(
            __('CMS Block with identifier "%1" does not exist.', $identifier)
        );
    }
    $content = $this->getBlockContentFiltered($block->getContent());
    $block->setContent($content);
    return $block;
}
/**
 * @inheritdoc
 */
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
{
    $storeId = $this->getStoreIdBySearchCriteria($searchCriteria);
    $this->emulation->startEnvironmentEmulation($storeId);
    /** @var \Magento\Cms\Model\ResourceModel\Block\Collection $collection */
    $collection = $this->blockCollectionFactory->create();
    $this->collectionProcessor->process($searchCriteria, $collection);
    $items = [];
    /** @var \Magento\Cms\Model\Block $block */
    foreach ($collection->getItems() as $block) {
        $content = $this->getBlockContentFiltered($block->getContent());
        $block->setContent($content);
        $blockDto = $this->blockDtoFactory->create();
        $this->dataObjectHelper->populateWithArray(
            $blockDto,
            $block->getData(),
            \Snowdog\CmsApi\Api\Data\BlockInterface::class
        );
        $blockDto->setId($block->getId());
        $items[] = $blockDto;
    }
    $searchResults = $this->searchResultsFactory->create();
    $searchResults->setSearchCriteria($searchCriteria);
    $searchResults->setItems($items);
    $searchResults->setTotalCount(count($items));
    $this->emulation->stopEnvironmentEmulation();
    return $searchResults;
}
/**
 * @param string $content
 * @return string
 */
private function getBlockContentFiltered($content)
{
    $emulatedResult = $this->appState->emulateAreaCode(
        Area::AREA_FRONTEND,
        [$this->filterProvider->getBlockFilter(), 'filter'],
        [$content]
    );
    return $emulatedResult;
}
}

/rest/V1/snowdog/cmsPage/:pageId: retrieves page info by its id (integer value)

Take refrance from Magento 2 CMS Api

I hope it helps!

  • its same code i have used same code – Sarvesh Tiwari Apr 1 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.