Skip to content

Commit

Permalink
Custom module to fetch Top Header API Details
Browse files Browse the repository at this point in the history
  • Loading branch information
Arjunkanani committed Feb 2, 2023
0 parents commit 9768dd5
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Api/CustomInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Binstellar\TopHeaderMenuAPI\Api;

interface CustomInterface
{

/**
* @return string
**/

public function getList();
}
47 changes: 47 additions & 0 deletions Model/TopHeaderMenuAPI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
namespace Binstellar\TopHeaderMenuAPI\Model;

use Magento\Cms\Block\Block as cmsBlock;
use Magento\Framework\Serialize\SerializerInterface;
use DOMDocument;

class TopHeaderMenuAPI implements \Binstellar\TopHeaderMenuAPI\Api\CustomInterface
{

/**
* @var SerializerInterface
*/
private $serializer;



public function __construct(
cmsBlock $cmsblock,
\Magento\Framework\Serialize\Serializer\Json $json
) {
$this->cmsblock = $cmsblock;
$this->json = $json;
}

/**
* @inheritdoc
*/
public function getList()
{

$dom = new DOMDocument;
$dom->loadHTML($this->getCmsBlock());

return $dom->textContent;

}

public function getCmsBlock()
{
$cmsblock = $this->cmsblock->setBlockId('top-header')->toHtml();
return $cmsblock;
}
}


?>
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## Magento2 How to create custom REST API to fetch details of CMS Block

> Magento2 Content Management System (CMS) Blocks also know as static blocks are used to store many dynamic information of the client which needs to be changed by client on regular bases. Same thing needs to be sometimes shared via Rest API.
Wondering how to achieve that? Don't worry we have got the solution for it.

> Install our module Binstellar/TopHeaderMenuAPI

## Installation Steps

Step 1 : Download the Zip file from Github & Unzip it
Step 2 : Create a directory under app/code/Binstellar/TopHeaderMenuAPI
Step 3 : Upload the files & folders from extracted package to app/code/Binstellar/TopHeaderMenuAPI
Step 4 : Go to the Magento2 Root directory & run following commands

php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy -f
php bin/magento cache:flush


## Note : We have tested this option in Magento ver. 2.4.5-p1
4 changes: 4 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?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="Binstellar\TopHeaderMenuAPI\Api\CustomInterface" type="Binstellar\TopHeaderMenuAPI\Model\TopHeaderMenuAPI"/>
</config>
5 changes: 5 additions & 0 deletions etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Binstellar_TopHeaderMenuAPI" setup_version="1.0.0">
</module>
</config>
9 changes: 9 additions & 0 deletions etc/webapi.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?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 method="GET" url="/V1/topheadermenu/">
<service class="Binstellar\TopHeaderMenuAPI\Api\CustomInterface" method="getList"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>
6 changes: 6 additions & 0 deletions registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Binstellar_TopHeaderMenuAPI',
__DIR__
);
1 change: 1 addition & 0 deletions view/frontend/templates/custom.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
XZXZX

0 comments on commit 9768dd5

Please sign in to comment.