Magento 2 自定义模块和 Web 服务返回空白数组

Jam*_*mer 5 php xml web-services magento magento2

我是 magento 2x 的新手。但是,我尝试创建一个自定义模块和一个网络服务来检查客户电子邮件是否存在。但结果我得到的只是一个空白数组。这是我到目前为止所做的:

1. app/code/Test/Customers/registration.php

\Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'Test_Customers',
        __DIR__
    );
Run Code Online (Sandbox Code Playgroud)

2. app/code/Test/Customers/etc/module.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"><module name="Test_Customers" setup_version="1.0.0"></module></config>
Run Code Online (Sandbox Code Playgroud)

3. app/code/Test/Customers/etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"><preference for="Test\Customers\Api\AccountInterface" type="Test\Customers\Model\Account"/></config>
Run Code Online (Sandbox Code Playgroud)

4. app/code/Test/Customers/etc/webapi.xml

<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd"><route url="/V1/customers/isEmailExist/" method="POST"><service class="Test\Customers\Api\AccountInterface" method="isEmailExist"/><resources><resource ref="anonymous"/></resources></route></routes>
Run Code Online (Sandbox Code Playgroud)

5. app/code/Test/Customers/Api/AccountInterface.php

    namespace Test\Customers\Api;
/**
 * Account interface.
 * @api
 */

interface AccountInterface
{
    /**
     * Check if given email is associated with a customer account in given website.
     * @api
     * @param string $customerEmail
     * @return \Test\Customers\Api\AccountInterface
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function isEmailExist($customerEmail);
}
Run Code Online (Sandbox Code Playgroud)

6. app/code/Test/Customers/Model/Account.php

namespace Test\Customers\Model;

use Test\Customers\Api\AccountInterface;
    use Magento\Customer\Api\CustomerRepositoryInterface;
    use Magento\Framework\Exception\NoSuchEntityException;

    class Account implements AccountInterface
    {

        /**
         * @var CustomerRepositoryInterface
         */
        private $customerRepository;

        /** @var \Magento\Framework\Controller\Result\JsonFactory */
        protected $resultJsonFactory;

        /**
         * @param CustomerRepositoryInterface $customerRepository
         * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
         */
        public function __construct(
            CustomerRepositoryInterface $customerRepository,
            \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
        )
        {
            $this->customerRepository = $customerRepository;
            $this->resultJsonFactory = $resultJsonFactory;
        }

        /**
         * {@inheritdoc}
         */
        public
        function isEmailExist($customerEmail)
        {
            /** @var \Magento\Framework\Controller\Result\JsonFactory */
            $result = $this->resultJsonFactory->create();
            try {
                $this->customerRepository->get($customerEmail);
            } catch (NoSuchEntityException $e) {

            }

            return $result->setData(['success' => true]);

        }
      }
Run Code Online (Sandbox Code Playgroud)

我尝试了来自 REST 客户端的 POST 请求

http://127.0.0.1/PATH_TO_MAGENTO_DIRECTORY/index.php/rest/V1/customers/isEmailExist

POST 正文:

{
    "customerEmail": "sa@example.com"
}
Run Code Online (Sandbox Code Playgroud)

并得到一个 [] 作为响应