magento2 在小程序模块增加 Api

  因公司开发需要,需要在官网心愿单的基础上增加小程序端心愿单功能,因与官网在模块及请求路径上有所不同,需要额外去开发一套代码功能。

一、踩得坑
1、magento2 接口必须添加注释,否则报错
magento2 在小程序模块增加 Api
2、在接口添加注释的时候,一定要注意参数的类型及返回数据的类型,否则报错
magento2 在小程序模块增加 Api
3、如果没有在 etc/di.xml中建立接口与模型的对应关系,会报错如下:
magento2 在小程序模块增加 Api
二、正确配置
1、创建接口:在 模块\Applet\Api 下创建接口文件 WishlistInterface.php (一定要注意参数类型与接口返回类型)
magento2 在小程序模块增加 Api
2、创建模型文件:在 模块\Applet\Model\Api 下创建模型文件 Wishlist.php
magento2 在小程序模块增加 Api
3、在 模块\Applet\etc\di.xml 增加接口与模型的对应关系
magento2 在小程序模块增加 Api

<preference for="模块\Applet\Api\WishlistInterface" type="模块\Applet\Model\Api\Wishlist"/>
4、为接口创建路由:在 模块\Applet\etc\webapi.xml 文件中

magento2 在小程序模块增加 Api

    <route url="/V1/applet/addWishlistProduct" method="POST">
        <service class="模块\Applet\Api\WishlistInterface" method="addWishlistProduct"/>
        <resources>
            <resource ref="self"/>
        </resources>
        <data>
            <parameter name="customerId" force="true">%customer_id%</parameter>
        </data>
    </route>
5、在模型中实现的代码如下
    public function removeWishlistProduct($customerId)
    {

    }
本作品采用《CC 协议》,转载必须注明作者和本文链接
May
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!