Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/sbboke.com/wp-content/plugins/wordpress-seo/src/integrations/front-end-integration.php:409) in /www/wwwroot/sbboke.com/wp-content/themes/pacify/content-single.php on line 5

Magento2 在rest api 中webapi.xml配置详解

要定义Web API组件,请在webapi.xml配置文件中的这些XML元素上设置这些属性 ,如下所示:

XML元素描述属性
<routes> 需要。根元素,用于定义XML模式文件的名称空间和位置。xmlns:xsi 需要。定义XML模式实例的名称空间。xsi:noNamespaceSchemaLocation 需要。定义用于验证Web API的XML模式文件的路径和文件名。
`<route>`需要。<routes>的子元素。定义Web API方法的HTTP路由。方法 需要。串。HTTP方法。有效值为GET,POST,PUT和DELETE。网址。需要。串。Magento资源的URL。字符串必须以`/ V1`(或`/ V <integer>`)开头以指示版本号。您必须在所有模板参数前加上一个冒号。范例:`/ V1 / products /:sku“安全”。可选的。布尔值。表示只能通过HTTPS访问该路由。通过非安全方式访问此路由的任何尝试都会导致异常。soapOperation。可选的。串。指定要使用的SOAP操作名称,而不是接口的方法名称。使用此元素可为同一服务合同创建多个操作。
`< service >`需要。<route>的子元素。定义实现的接口和Web API方法名称。类。需要。串。实现接口的位置和名称。方法 需要。串。Web API方法名称。
< resources >需要。<route>的子元素。一个或多个资源定义的容器。没有。
`< resource >`需要。<< resources>`的子元素。定义调用者必须有权访问的资源。ref。需要。参考资源。有效值为“ self”,“ anonymous”或Magento资源,例如“ Magento_Customer :: group”。 注意:Magento Web API框架使来宾用户可以访问配置了“匿名”权限的资源。框架无法通过现有身份验证机制进行身份验证的任何用户都被视为来宾用户。
`< data >`可选的。<route>的子元素。一个或多个参数定义的容器。没有。
`< parameter >`如果指定了<data>,则为必需。<data>的子元素。定义一个参数。名称。串。参数名称。力 布尔值。强制请求参数

样本webapi.xml文件

此摘录来自webapi.xml定义客户服务Web API 的文件:

<?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/customerGroups/:id" method="GET">
        <service class="Magento\Customer\Api\GroupRepositoryInterface" method="getById"/>
        <resources>
            <resource ref="Magento_Customer::group"/>
        </resources>
    </route>
...
    <route url="/V1/customers/me/billingAddress" method="GET">
        <service class="Magento\Customer\Api\AccountManagementInterface" method="getDefaultBillingAddress"/>
        <resources>
            <resource ref="self"/>
        </resources>
        <data>
            <parameter name="customerId" force="true">%customer_id%</parameter>
        </data>
    </route>
</routes>

在此webapi.xml示例中:

字段定义
routes用于验证XML的XML模式文件。XML模式文件是`xmlns:xsi =“ http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation =“ urn:magento:module:Magento_Webapi:etc / webapi.xsd”。
route通过其访问路由的HTTP方法和Web资源。HTTP方法是GET。资源是`/ V1 / customerGroups /:id`。用户必须用客户ID代替`id`模板参数。
service路由实现的接口和Web API方法的名称。该路由实现`Magento \ Customer \ Api \ GroupRepositoryInterface`接口。Web API方法名称为`getById`。
resource调用者必须有权访问的资源。调用者必须有权访问`Magento_Customer :: group`资源。
route必需的参数。在对`/ V1 / customers / me / billingAddress`的GET调用中,需要`id`参数。

webapi.xsd XML模式文件

webapi.xml文件为您的模块必须指定验证XML架构文件。您的webapi.xml文件可以指定默认或自定义的XML模式文件。webapi.xsd可以在app/code/Magento/Webapi/etc目录中找到默认的XML模式文件。

下表定义了service节点属性:

属性名称需要描述
class处理API请求的负责任的类。
method处理API执行的“类”方法。

强制请求参数

中的参数webapi.xml可以强制使用。这样可以确保在特定路由上始终使用特定值。例如,在上面的示例“ / V1 / customers / me / billingAddress”路由中,该customerId 参数被强制匹配当前登录用户的ID。可以通过di.xml向的paramOverriders参数添加新项来 注册其他参数替代\Magento\Webapi\Controller\Rest\ParamsOverrider。参数替代程序必须实现\Magento\Framework\Webapi\Rest\Request\ParamOverriderInterface。摘录的示例di.xml

<type name="Magento\Webapi\Controller\Rest\ParamsOverrider">
    <arguments>
        <argument name="paramOverriders" xsi:type="array">
            <item name="%my_value%" xsi:type="object">VENDOR\MODULE\Controller\Rest\ParamOverriderMyValue</item>
        </argument>
    </arguments>
</type>

上面的示例创建了一个可用于中的新参数替代webapi.xml。传递的值 %my_value%将是的返回值 \VENDOR\MODULE\Controller\Rest\ParamOverriderMyValue::getOverriddenValue。例:

<route url="/V1/example/me/service" method="GET">
    ...
    <data>
        <parameter name="myValue" force="true">%my_value%</parameter>
    </data>
    ...
</route>

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论