我是 magento 2x 的新手。不过,我尝试创建一个自定义模块和一个网络服务来检查客户电子邮件是否存在。但结果我只得到一个空白数组。这是我到目前为止所做的:
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。应用程序/代码/测试/客户/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);
}
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
帖子正文:
{
"customerEmail": "[email protected]"
}
并得到了 [] 作为回复
在文件 app/code/Test/Customers/Api/AccountInterface.php 中
您已使用 \Test\Customers\Api\AccountInterface 作为返回,但您正在返回数组,因此您应该使用 * @return 数组。
示例:
/**
* Check if given email is associated with a customer account in given website.
* @api
* @param string $customerEmail
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
*/
如果您不确定输出是什么,请使用混合而不是数组
尝试返回 json as 并使用字符串作为注释中的返回:
return $returnArray = json_encode($response);
希望有帮助!!