我是magento 2x的新手。但是,我尝试创建自定义模块和Web服务来检查客户电子邮件是否存在。但结果我得到的只是一个空白数组。以下是我到目前为止所做的事情:
1。应用程序/代码/测试/客户/和registration.php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Test_Customers',
__DIR__
);
2。应用程序/代码/测试/顾客的/ 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>
第3。应用程序/代码/测试/顾客的/ 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>
4。应用程序/代码/测试/顾客的/ 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>
5。应用程序/代码/测试/客户/原料药/ 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);
}
6。应用程序/代码/测试/客户/型号/ 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]);
}
}
我尝试了来自REST客户端的POST请求
http://127.0.0.1/PATH_TO_MAGENTO_DIRECTORY/index.php/rest/V1/customers/isEmailExist
POST Body:
{
"customerEmail": "sa@example.com"
}
并得到一个[]作为回应