在 magento 2 中添加自定义变量以在 REST 中使用 API
Add custom variables in magento 2 to use in REST API
我在我的网站中使用了一些数据,例如社交链接、联系地址、联系人 phone、滑块横幅,我可以将它们用作块或联系页面中的 html。但是我遇到了一个问题,如何将它们称为 REST API。我已经在使用 Magento2 API:
/V1/cmsBlock/:blockId
/V1/cmsPage/:pageId
但是响应是 html,而且非常糟糕。有帮助吗?
对于社交链接、联系电话等数据,我建议您将其作为文本添加到配置中,为此您可以创建一个具有以下结构的模块:
app/code/Jsparo/Customapi/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Jsparo_Customapi',
__DIR__
);
app/code/Jsparo/Customapi/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Jsparo_Customapi" setup_version="1.0.0">
</module>
</config>
app/code/Jsparo/Customapi/etc/adminhtml/system.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="jsparo" translate="label" sortOrder="1100">
<label>Jsparo</label>
</tab>
<section id="jsparo_social" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Social</label>
<tab>jsparo</tab>
<resource>Jsparo_Social::config</resource>
<group id="facebook" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Facebook</label>
<field id="url" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Facebook Url</label>
</field>
</group>
</section>
</system>
</config>
app/code/Jsparo/Customapi/etc/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">
<route url="/V1/jsparo/facebook" method="GET">
<service class="Jsparo\Customapi\Api\FacebookInterface" method="getUrl"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>
app/code/Jsparo/Customapi/etc/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="Jsparo\Customapi\Api\FacebookInterface" type="Jsparo\Customapi\Model\Facebook"/>
</config>
app/code/Jsparo/Customapi/Api/FacebookInterface.php
<?php
namespace Jsparo\Customapi\Api;
interface FacebookInterface {
/**
* @return string $url
*/
public function getUrl();
}
app/code/Jsparo/Customapi/Helper/Data.php
<?php
namespace Jsparo\Customapi\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
class Data extends AbstractHelper {
const prefix = 'jsparo_social/';
private function moduleConfig($key) {
return $this->scopeConfig->getValue(self::prefix . $key);
}
public function getFacebookUrl() {
return $this->moduleConfig('facebook/url');
}
}
app/code/Jsparo/Customapi/Model/Facebook.php
<?php
namespace Jsparo\Customapi\Model;
use Jsparo\Customapi\Helper\Data;
class Facebook implements FacebookInterface {
private $helper;
public function __construct(
Data $helper
) {
$this->helper = $helper;
}
public function getUrl() {
return $this->helper->getFacebookUrl();
}
}
您可能需要做一些调整并添加您需要的所有字段/api 端点。
您还可以通过使用 Magento\Framework\App\CacheInterface
将缓存添加到您的 API 以避免必须执行某些计算。
请注意,我使用 anonymous
角色创建了端点,因此它不受保护。
编辑:我创建了一个 github repository,您可以在其中查看完整的源代码并编辑了上面的几个拼写错误。我假设此模块源代码已添加到 app/code/Jsparo/Customapi
.
我在我的网站中使用了一些数据,例如社交链接、联系地址、联系人 phone、滑块横幅,我可以将它们用作块或联系页面中的 html。但是我遇到了一个问题,如何将它们称为 REST API。我已经在使用 Magento2 API:
/V1/cmsBlock/:blockId
/V1/cmsPage/:pageId
但是响应是 html,而且非常糟糕。有帮助吗?
对于社交链接、联系电话等数据,我建议您将其作为文本添加到配置中,为此您可以创建一个具有以下结构的模块:
app/code/Jsparo/Customapi/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Jsparo_Customapi',
__DIR__
);
app/code/Jsparo/Customapi/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Jsparo_Customapi" setup_version="1.0.0">
</module>
</config>
app/code/Jsparo/Customapi/etc/adminhtml/system.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="jsparo" translate="label" sortOrder="1100">
<label>Jsparo</label>
</tab>
<section id="jsparo_social" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Social</label>
<tab>jsparo</tab>
<resource>Jsparo_Social::config</resource>
<group id="facebook" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Facebook</label>
<field id="url" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Facebook Url</label>
</field>
</group>
</section>
</system>
</config>
app/code/Jsparo/Customapi/etc/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">
<route url="/V1/jsparo/facebook" method="GET">
<service class="Jsparo\Customapi\Api\FacebookInterface" method="getUrl"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>
app/code/Jsparo/Customapi/etc/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="Jsparo\Customapi\Api\FacebookInterface" type="Jsparo\Customapi\Model\Facebook"/>
</config>
app/code/Jsparo/Customapi/Api/FacebookInterface.php
<?php
namespace Jsparo\Customapi\Api;
interface FacebookInterface {
/**
* @return string $url
*/
public function getUrl();
}
app/code/Jsparo/Customapi/Helper/Data.php
<?php
namespace Jsparo\Customapi\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
class Data extends AbstractHelper {
const prefix = 'jsparo_social/';
private function moduleConfig($key) {
return $this->scopeConfig->getValue(self::prefix . $key);
}
public function getFacebookUrl() {
return $this->moduleConfig('facebook/url');
}
}
app/code/Jsparo/Customapi/Model/Facebook.php
<?php
namespace Jsparo\Customapi\Model;
use Jsparo\Customapi\Helper\Data;
class Facebook implements FacebookInterface {
private $helper;
public function __construct(
Data $helper
) {
$this->helper = $helper;
}
public function getUrl() {
return $this->helper->getFacebookUrl();
}
}
您可能需要做一些调整并添加您需要的所有字段/api 端点。
您还可以通过使用 Magento\Framework\App\CacheInterface
将缓存添加到您的 API 以避免必须执行某些计算。
请注意,我使用 anonymous
角色创建了端点,因此它不受保护。
编辑:我创建了一个 github repository,您可以在其中查看完整的源代码并编辑了上面的几个拼写错误。我假设此模块源代码已添加到 app/code/Jsparo/Customapi
.