Skip to content

Commit 9768dd5

Browse files
committed
Custom module to fetch Top Header API Details
0 parents  commit 9768dd5

File tree

8 files changed

+108
-0
lines changed

8 files changed

+108
-0
lines changed

Api/CustomInterface.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Binstellar\TopHeaderMenuAPI\Api;
4+
5+
interface CustomInterface
6+
{
7+
8+
/**
9+
* @return string
10+
**/
11+
12+
public function getList();
13+
}

Model/TopHeaderMenuAPI.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
namespace Binstellar\TopHeaderMenuAPI\Model;
3+
4+
use Magento\Cms\Block\Block as cmsBlock;
5+
use Magento\Framework\Serialize\SerializerInterface;
6+
use DOMDocument;
7+
8+
class TopHeaderMenuAPI implements \Binstellar\TopHeaderMenuAPI\Api\CustomInterface
9+
{
10+
11+
/**
12+
* @var SerializerInterface
13+
*/
14+
private $serializer;
15+
16+
17+
18+
public function __construct(
19+
cmsBlock $cmsblock,
20+
\Magento\Framework\Serialize\Serializer\Json $json
21+
) {
22+
$this->cmsblock = $cmsblock;
23+
$this->json = $json;
24+
}
25+
26+
/**
27+
* @inheritdoc
28+
*/
29+
public function getList()
30+
{
31+
32+
$dom = new DOMDocument;
33+
$dom->loadHTML($this->getCmsBlock());
34+
35+
return $dom->textContent;
36+
37+
}
38+
39+
public function getCmsBlock()
40+
{
41+
$cmsblock = $this->cmsblock->setBlockId('top-header')->toHtml();
42+
return $cmsblock;
43+
}
44+
}
45+
46+
47+
?>

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## Magento2 How to create custom REST API to fetch details of CMS Block
2+
3+
> 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.
4+
5+
Wondering how to achieve that? Don't worry we have got the solution for it.
6+
7+
> Install our module Binstellar/TopHeaderMenuAPI
8+
9+
10+
## Installation Steps
11+
12+
Step 1 : Download the Zip file from Github & Unzip it
13+
Step 2 : Create a directory under app/code/Binstellar/TopHeaderMenuAPI
14+
Step 3 : Upload the files & folders from extracted package to app/code/Binstellar/TopHeaderMenuAPI
15+
Step 4 : Go to the Magento2 Root directory & run following commands
16+
17+
php bin/magento setup:upgrade
18+
php bin/magento setup:di:compile
19+
php bin/magento setup:static-content:deploy -f
20+
php bin/magento cache:flush
21+
22+
23+
## Note : We have tested this option in Magento ver. 2.4.5-p1

etc/di.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
3+
<preference for="Binstellar\TopHeaderMenuAPI\Api\CustomInterface" type="Binstellar\TopHeaderMenuAPI\Model\TopHeaderMenuAPI"/>
4+
</config>

etc/module.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
3+
<module name="Binstellar_TopHeaderMenuAPI" setup_version="1.0.0">
4+
</module>
5+
</config>

etc/webapi.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0"?>
2+
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
3+
<route method="GET" url="/V1/topheadermenu/">
4+
<service class="Binstellar\TopHeaderMenuAPI\Api\CustomInterface" method="getList"/>
5+
<resources>
6+
<resource ref="anonymous"/>
7+
</resources>
8+
</route>
9+
</routes>

registration.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
\Magento\Framework\Component\ComponentRegistrar::register(
3+
\Magento\Framework\Component\ComponentRegistrar::MODULE,
4+
'Binstellar_TopHeaderMenuAPI',
5+
__DIR__
6+
);

view/frontend/templates/custom.phtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
XZXZX

0 commit comments

Comments
 (0)