magento2 开发Api接口实例

8.75K 浏览开发笔记api

magento2 开发Api接口实例

新建一个模块

第一步先创建module.xml初始化模块

模块配置 – etc/module.xml

<?xml version="1.0"?> 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">  
    <module name="Max_Hello" setup_version="1.0.0" /> 
</config>

然后新建Registration

注册模块 – registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Max_Hello',
__DIR__
);

API 的配置

这里需要建立两个xml文件di.xml和webapi.xml,其中di用于依赖注入,而webapi用于设定路由和指定方法名称,同时设定访问权限

Web API 配置 – 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/hello/name/:name" method="GET">
    <service class="Max\Hello\Api\HelloInterface" method="name"/>    
    <resources>
        <resource ref="anonymous"/> 
    </resources>
</route>
</routes>

我们使用anonymous设置,让其可以直接通过url访问

注入声名 – 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="Max\Hello\Api\HelloInterface" type="Max\Hello\Model\Hello" />  
</config>

创建接口文件 – Api/HelloInterface.php

<?php 
namespace Max\Hello\Api;  
interface HelloInterface{         
/**      
* Returns greeting message to user      
*      * @api      
* @param string $name Users name.      
* @return string Greeting message with users name.      
*/     
public function name($name); 
}

新建Model – Model/Hello.php

<?php 
namespace Max\Hello\Model; 
use Max\Hello\Api\HelloInterface;  
class Hello implements HelloInterface{        
/**      
* Returns greeting message to user      
*      * @api      
* @param string $name Users name.      
* @return string Greeting message with users name.      
*/     
public function name($name) {                 
    return "Hello, " . $name;     
} 
}

这里必须在声名方法前加上正确格式的注释,注明参数类型和返回类型,不然接口会报Class does not exist

这我猜测因为它是基于soap的接口,但php是弱类型命名的,所以在类似WSDL中其他强类型命名的想调用,出于考虑Magento强制把类型定义放到注释上,但这是一个大坑,如果不知道的人很可能也会遇到这个问题。

测试Rest Api

Rest Api格式如下:
http://{domain_name}/rest/V1/{method}/{attribute}/{value}
浏览器直接打开地址如下:
如: http://magento2.dev/rest/V1/hello/name/world

浏览器会显示以下结果:

<response>Hello, world</response>

SOAP方式访问:

<?php 
$proxy = new SoapClient('http://magento2.dev/index.php/soap/default?wsdl&services=maxHelloV1'); 
$result = $proxy->maxHelloV1Name(array("name"=>"world")); 
var_dump($result);

SOAP打印结果

object(stdClass)#2 (1) {   ["result"]=>  string(10) "Hello, world"}

ACL.XML

若不在WebApi使用anonymous权限,我们需要在etc文件夹新建一个acl.xml文件

如: – etc/acl.xml

<?xml version="1.0"?> 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nOnamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">     
<acl>         
<resources>             
<resource id="Magento_Backend::admin">                 
<resource id="Max_Hello::hello" title="Hello" translate="title" sortOrder="110" />             
</resource>         
</resources>     
</acl> 
</config>

在这种情况下,我们需要在webapi.xml的resource节点中添加“Max_Hello ::hello”,这种操作后就可以不使用anonymous了。

参考:

http://magento.stackexchange.com/questions/85017/report-id-webapi-560e30f44811c-message-class-name-does-not-exis-in-magento2

编辑问题
1

出现class does not exist, 在方法上面需要确定参数值的类型和名称,返回值的类型。具体可以看官方文档https://devdocs.magento.com/guides/v2.3/coding-standards/docblock-standard-general.html

0
您正在查看1个回答中的1个,单击此处查看所有回答。