分类
后端

magento2 rest API 开发

1. 新建或者修改 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/cmsPage/:identifier/:code" method="GET">
        <service class="Maijindou\Customer\Api\CmsPageInterface" method="getPage"/>
        <resources>
            <resource ref="anonymous" />
        </resources>
    </route>
</routes>

2. 新建或者修改di.xml

<config>
<preference for="Maijindou\Customer\Api\CmsPageInterface" type="Maijindou\Customer\Model\CmsPage"/>
</config>

3. 新建API接口文件

# Maijindou/Customer/Api/CmsPageInterface.php
# 注意必须写注释 传入的参数必须定义参数类型
namespace Maijindou\Customer\Api;


interface CmsPageInterface
{
    /**
     * 获取cms页面内容
     * @param string $identifier
     * @param string $code
     * @return \Maijindou\Customer\Api\CmsPageInterface[]
     * @throws \Magento\Framework\Exception\LocalizedException
     * @since : 2020/5/26 14:57
     * @author: XiaKai
     * @email: 1030989241@qq.com
     */
    public function getPage($identifier,$code);
}

4. 新建API执行文件

# Maijindou/Customer/Model/CmsPage.php
# 注意必须写注释 传入的参数必须定义参数类型
namespace Maijindou\Customer\Model;
use Maijindou\Customer\Api\CmsPageInterface;

class CmsPage implements CmsPageInterface
{

    /**
     * 获取cms页面内容
     * @param string $identifier
     * @param string $code
     * @return \Maijindou\Customer\Api\CmsPageInterface[]
     * @throws \Magento\Framework\Exception\LocalizedException
     * @since : 2020/5/26 14:57
     * @author: XiaKai
     * @email: 1030989241@qq.com
     */
    public function getPage($identifier, $code)
    {
        echo 1111;exit;
    }
}