[译]Magento2中使用Web Api

news/2024/7/3 17:42:54

今天我开发一个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....


http://www.niftyadmin.cn/n/4290253.html

相关文章

mongodb java分页查询_java使用mongodb分页查询案例

Overridepublic Response chipListByPage(ChipPageListParam request) {//查询条件Query query new Query();Criteria criteria new Criteria();if (StringUtils.isNotBlank(request.getTitleLike())) {Pattern pattern Pattern.compile("^.*" request.getTitleL…

java data jpa_什么是Spring Data JPA?Java程序员需要了解的

Spring Data JPA(Java Persistence API)&#xff0c;是Spring框架的主要构建块之一。如果您想使用持久数据&#xff0c;它也是一个强大的工具。我经常看到实际使用它的开发人员看不到全局&#xff0c;他们错过了它的一些最有用的能力。因此&#xff0c;我想向您展示处理应用程序…

c rsa java_[悬赏]一个RSA的c++和Java版本不能互通,头发都白了

这是客户端代码&#xff0c;公钥加密&#xff0c;私钥解密。公钥由服务器端提供,加密后数据用Base64编码发送出去。用Java代码是没有问题的&#xff0c;用C版本对方就解密失败了。C平台使用的是OPenSSL库。都使用PKCS1填充模式。找了几天没找出问题&#xff0c;什么大端&#x…

Java内存管理及GC算法

概述 内存划分 虚拟机规范中将内存分为六大部分&#xff0c;分别为PC寄存器、JAVA虚拟机栈、JAVA堆、方法区、运行时常量及本地方法栈。 1.PC寄存器&#xff1a;线程独占&#xff1b; 2.JAVA虚拟机栈&#xff1a;线程独有&#xff1b;JAVA虚拟机栈是在创建线程的同时创建的&…

shell实现多级菜单脚本编写

这篇文章主要介绍了Shell实现多级菜单系统安装脚本实例分享,本文脚本用多级菜单实现LAMP、LNMP安装展现效果,需要的朋友可以参考下&#xff1a;提示&#xff1a;本脚本主要实现多级菜单效果&#xff0c;并没有安装LAMP、LNMP环境&#xff0c;如果要用在实际生成环境中部署LNMP、…

java 蒙特卡洛_java算法3_蒙特卡洛方法(Monte Carlo method)求PI和椭圆面积

蒙特卡洛方法&#xff0c;是一种以概率统计理论为指导的一类非常重要的数值计算方法。是指使用随机数来解决很多计算问题的方法。蒙特卡洛方法的名字来源于摩纳哥的一个城市蒙特卡洛&#xff0c;该城市以业闻名&#xff0c;而蒙特卡洛方法正是以概率为基础的方法。这里我们使用…

Quartz.net Trigger触发器下 Cron表达式的格式

有位博主写的不错&#xff0c;样式标准好理解&#xff0c;借鉴下。 foamflower 1、 CronTrigger时间格式配置说明 CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年] 序号 说明 是否必填 允许填写的值 允许的通配符 1 秒 是 0-59 , - * /…

java 0l_Java Synchronized

1、synchronized 同步方法&#xff1a;是对当前对象加锁。packagecom.test;public classTestObject {synchronized public voidmethodA() {try{System.out.println("begin methodA threadName" Thread.currentThread().getName() " beigin time " Syste…