[译]Magento2中使用Web Api

今天我开发一个Magento2的Webapi来分享一下

新建一个模块

假设我们已经学习过建设模块的前提
第一部建设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="Inchoo_Hello" setup_version="1.0.0" />
</config>

然后新建Registration

注册模块 – registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Inchoo_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="Inchoo\Hello\Api\HelloInterface" method="name"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

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

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

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

<?php
namespace Inchoo\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 Inchoo\Hello\Model;
use Inchoo\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
我就遇上这个坑了后来网上找到:http://magento.stackexchange....
在接口文件加注释声名参数类型后可以正常运行,这我猜测是因为它是基于soap的接口,但php是弱类型命名的,所以在类似WSDL中其他强类型命名的想调用,出于考虑Magento把类型定义放到注释上,但这是一个大坑,我们这些不清楚的人会不知道这个问题

目录结构如下图:
clipboard.png

测试Rest Api

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

浏览器会显示以下结果:

<response>Hello, Jim</response>

SOAP方式访问:

<?php
$proxy = new SoapClient('http://magento2.vm/index.php/soap/default?wsdl&services=inchooHelloV1');
$result = $proxy->inchooHelloV1Name(array("name"=>"Jim"));
var_dump($result);

SOAP打印结果

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

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="Inchoo_Hello::hello" title="Hello" translate="title" sortOrder="110" />
            </resource>
        </resources>
    </acl>
</config>

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

参考:http://inchoo.net/magento/api...
http://magento.stackexchange....

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值