简体   繁体   English

在magento 2中添加自定义变量以在REST API中使用

[英]Add custom variables in magento 2 to use in REST API

I am using some data in my web site like social links, contact address, contact phone, slider banners, I can use them as html in blocks or contact pages. 我在网站上使用某些数据,例如社交链接,联系地址,联系电话,滑块横幅,我可以将它们用作html块或联系页面。 But I am facing a problem, How to call them as REST API. 但是我面临一个问题,如何将它们称为REST API。 I Already uses Magento2 API: 我已经使用了Magento2 API:

/V1/cmsBlock/:blockId 
/V1/cmsPage/:pageId

But the respobse is html and it is so bad. 但是,respobse是html,非常糟糕。 any help? 有什么帮助吗?

For the data like social links, contact numbers etc I suggest you to add this as text in the configuration, to do so you can create a module that has the following structure: 对于诸如社交链接,联系电话等数据,我建议您将其作为文本添加到配置中,这样做可以创建具有以下结构的模块:

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();
    }
}

You might have to do some adjustments and add all the fields / api endpoints that you need to it. 您可能需要做一些调整,并将所有需要的字段/ api端点添加到其中。

You can add also Caching to your API by using the Magento\\Framework\\App\\CacheInterface to avoid having to perform certain computations. 您还可以使用Magento\\Framework\\App\\CacheInterface将缓存添加到API中,以避免必须执行某些计算。

Notice that I created the endpoint with the anonymous role, so that it is not protected. 请注意,我使用anonymous角色创建了端点,因此该端点不受保护。

EDIT: I created a github repository where you can see the full source and edited the couple typos that were above. 编辑:我创建了一个github存储库 ,您可以在其中看到完整的源代码,并编辑了上面的几个错别字。 I assume that this module source code gets added in app/code/Jsparo/Customapi . 我假设此模块源代码已添加到app/code/Jsparo/Customapi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM