[譯]Magento2中使用Web Api

今天我開發一個Magento2的Webapi來分享一下php

新建一個模塊

假設咱們已經學習過建設模塊的前提
第一部建設module.xml設定模塊web

模塊配置 – 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>

而後新建Registrationapi

註冊模塊 – 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設置,讓其能夠直接訪問dom

注入聲名 – 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... url

瀏覽器會顯示如下結果:spa

<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文件.net

如: – 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....

相關文章
相關標籤/搜索