一、创建API请求

创建API请求有4个简单步骤:

第1步:声明API

在模块的etc文件夹下创建webapi.xml:
/magento/module-customer/etc/webapi.xml

<route url="/V1/customers" method="POST">
   <service class="Magento\Customer\Api\AccountManagementInterface" method="createAccount"/>
   <resources>
       <resource ref="anonymous"/>
   </resources>
</route>

让我们引导您完成上述代码,以确保您了解正在发生的事情:

  • Route - 这是用于调用我们的API URL https://{{MagentoBaseURL}}/index.php/rest/V1/customers.。
  • Service class - 这是我们API的接口类,将调用主要方法“createAccount”。
  • Resources - 定义谁有权限调用此API。它可以是具有特定权限的匿名(每个人)或自己(客户)或特定管理员用户,例如,可以在acl.xml中添加Magento_Customer:: customer。

步骤2:创建webapi.xml中指定的接口文件

Magento\Customer\Api\CustomerRepositoryInterface

/**
* Create customer account. Perform necessary business operations like sending email.
*
* @param \Magento\Customer\Api\Data\CustomerInterface $customer
* @param string $password
* @param string $redirectUrl
* @return \Magento\Customer\Api\Data\CustomerInterface
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function createAccount(
   \Magento\Customer\Api\Data\CustomerInterface $customer,
   $password = null,
   $redirectUrl = ''
);

第3步:创建模型类,您可以在其中放置实际的业务逻辑

您必须在etc文件夹下的di.xml文件中指定它
/module-customer/etc/di.xml

<preference for="Magento\Customer\Api\AccountManagementInterface"
           type="Magento\Customer\Model\AccountManagement" />

 

步骤4:创建第一个模型类AccountManagement.php以定义createAccount方法

/**
* @inheritdoc
*/
public function createAccount(CustomerInterface $customer, $password = null, $redirectUrl = '')
{
   if ($password !== null) {
       $this->checkPasswordStrength($password);
       $customerEmail = $customer->getEmail();
       try {
           $this->credentialsValidator->checkPasswordDifferentFromEmail($customerEmail, $password);
       } catch (InputException $e) {
           throw new LocalizedException(
               __("The password can't be the same as the email address. Create a new password and try again.")
           );
       
       $hash = $this->createPasswordHash($password);
   } else {
       $hash = null;
   
   return $this->createAccountWithPasswordHash($customer, $hash, $redirectUrl);
}

 

二、强制请求参数

我们可以强制webapi.xml中的参数来保证在特定路由上使用特定值。让我们看一下/V1/carts/mine的例子

第1步:在webapi.xml中声明参数

vendor/magento/module-quote/etc/webapi.xml

<route url="/V1/carts/mine" method="POST">
   <service class="Magento\Quote\Api\CartManagementInterface" method="createEmptyCartForCustomer"/>
   <resources>
       <resource ref="self" />
   </resources>
   <data>
       <parameter name="customerId" force="true">%customer_id%</parameter>
   </data>
</route>

 

第2步:在di.xml中进行覆盖

vendor/magento/module-webapi/etc/di.xml

<type name="Magento\Webapi\Controller\Rest\ParamsOverrider">
   <arguments>
       <argument name="paramOverriders" xsi:type="array">
           <item name="%customer_id%" xsi:type="object">Magento\Webapi\Controller\Rest\ParamOverriderCustomerId</item>
       </argument>
   </arguments>
</type>

第3步:获取params的值

vendor/magento/module-webapi/Controller/Rest/ParamOverriderCustomerId.php

/**
* {@inheritDoc}
*/
public function getOverriddenValue()
{
   if ($this->userContext->getUserType() === UserContextInterface::USER_TYPE_CUSTOMER) {
       return $this->userContext->getUserId();
   
   return null;
}

我们已向您提供了为Magento 2创建API请求的基本指南。希望你能帮助到您。