1. 配置一个webapi.xml
- 添加一个webapi.xml文件到app/code/自定义模块/etc目录
 
<?xml version="1.0"?>
    <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <!-- Customer Group Service-->
    <route url="/V1/packout/user" method="GET">
        <service class="Tti\Packout\Api\UserInterface" method="getUserInfo"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>
 
2. 添加一个di.xml文件到模块的etc目录下
<?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="Tti\Packout\Api\UserInterface" type="Tti\Packout\Model\Api\User"/>
</config>
 
3. 添加webapi.xml文件定义的接口文件
- 在模块根目录下添加Api目录,添加示例文件UserInterface.php到该目录
 
<?php
namespace Tti\Packout\Api;
/**
 * Get for user api
 * 
 * @return string
 */
interface UserInterface {
    public function getUserInfo();
}
 
4. 添加di.xml文件定义的model文件User.php
- 在模块根目录下添加Model\Api目录,添加文件User.php到该目录
 
<?php
namespace Tti\Packout\Model\Api;
class User {
    public function getUserInfo()
    {
        return json_encode(["name" => 'zhangsan']);
    }
}
 
5. 从后台更新缓存
System->Cache Manage 勾选所有选项刷新










