需要为 Magento 2 中的评论创建自定义 REST API
Need of create custom REST API for reviews in Magento 2
我需要在 magento 2 中创建一个 REST API,它将商店 ID 作为参数,returns 特定商店 ID 的所有评论。
如果传递附加参数,如客户 ID、产品 ID,则应执行过滤器。
这是一个需要解释的非常宽泛的话题。我正在逐步解释程序。
步骤 1. 在 Magento 2 核心文件夹中转到 app/code。
创建像 ECMAG 这样的供应商文件夹和像 MyReviews 这样的子文件夹。
在 MyReviews 文件夹中创建三个文件夹 Api,etc 和 model.
第 2 步。在 etc 文件夹中,使用以下代码创建 di.xml 文件。
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<preference for="ECMAG\MyReviews\Api\MyReviewInterface"
type="ECMAG\MyReviews\Model\MyReviewClass" />
</config>
接下来,在同一文件夹中创建 module.xml 文件。
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="ECMAG_MyReviews" setup_version="1.0.0"/>
</config>
接下来,在同一文件夹中创建 webapi.xml 文件。
<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd">
<route url="/V1/showreview/allreviews/:storeId" method="GET">
<service class="ECMAG\MyReviews\Api\MyReviewInterface" method="getAllReviews"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>
第 3 步。接下来在 Api 文件夹中创建界面,例如。
<?php
namespace ECMAG\MyReviews\Api;
interface MyReviewInterface
{
/**
* GET review by its ID
*
* @api
* @param string $storeId
* @return array
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getAllReviews($storeId);
}
第 4 步。接下来在模型文件夹中创建 class。
<?php
namespace ECMAG\MyReviews\Model;
use ECMAG\MyReviews\Api\MyReviewInterface;
use Magento\Framework\App\Bootstrap;
class MyReviewClass implements MyReviewInterface{
protected $request;
public function __construct(\Magento\Framework\App\Request\Http $request) {
$this->request = $request;
}
/**
* GET review by its ID
*
* @api
* @param string $storeId
* @return array
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getAllReviews($storeId){
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
$currentStoreId = $storeManager->getStore()->getId();
$rating = $objectManager->get("Magento\Review\Model\ResourceModel\Review\CollectionFactory");
//Apply filter for store id and status='Approved'
$collection = $rating->create()->addStoreFilter($storeId
)->addStatusFilter(\Magento\Review\Model\Review::STATUS_APPROVED);
//Get All parameters from request
$allParameters=$this->request->getParams();
//Check parameter from_Date present or not
if(array_key_exists("fromDate",$allParameters)){
$collection=$collection->addFieldToFilter('created_at', ['gteq' => $allParameters['fromDate']]);
}
//Check parameter to_Date present or not
if(array_key_exists("toDate",$allParameters)){
$collection=$collection->addFieldToFilter('created_at', ['lteq' => $allParameters['toDate']]);
}
//Check parameter title present or not
if(array_key_exists("title",$allParameters)){
$title=$allParameters['title'];
$collection=$collection->addFieldToFilter('title', ['like' => '%'.$title.'%']);
}
//Check parameter text present or not
if(array_key_exists("text",$allParameters)){
$collection=$collection->addFieldToFilter('detail', ['like' => '%'.$allParameters['text'].'%']);
}
//Check parameter customer id present or not
if(array_key_exists("customerId",$allParameters)){
$collection=$collection->addFieldToFilter('customer_id', ['eq' => $allParameters['customerId']]);
}
//Check parameter product id present or not
if(array_key_exists("productId",$allParameters)){
$collection=$collection->addFieldToFilter('entity_pk_value', ['eq' => $allParameters['productId']]);
}
//Check paramter for maximum no. of product per page
if(array_key_exists("pageSize",$allParameters)){
$collection->setPageSize($allParameters['pageSize']);
}
//Check paramter for current page no.
if(array_key_exists("page",$allParameters)){
$collection->setCurPage($allParameters['page']);
}
$result=$collection->getData();
return $result;
}
}
在上述方法中,首先对 Store ID 应用过滤器,然后传入可选参数 URL,如 customer_id、product_id。
参数名称的特殊检查。
第 5 步。最后在 MyReviews 文件夹中创建 registration.php。
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'ECMAG_MyReviews',
__DIR__
);
并调用系统升级命令-> php bin/magento setup:upgrade
在所有上述过程调用 url 之后 -`http://hostname/magento/index.php/rest/V1/showreview/allreviews/1'
在 url 中传递参数时也像-
`http://hostname/magento/index.php/rest/V1/showreview/allreviews/1?productId=1'
希望对您有所帮助。
我需要在 magento 2 中创建一个 REST API,它将商店 ID 作为参数,returns 特定商店 ID 的所有评论。
如果传递附加参数,如客户 ID、产品 ID,则应执行过滤器。
这是一个需要解释的非常宽泛的话题。我正在逐步解释程序。
步骤 1. 在 Magento 2 核心文件夹中转到 app/code。 创建像 ECMAG 这样的供应商文件夹和像 MyReviews 这样的子文件夹。 在 MyReviews 文件夹中创建三个文件夹 Api,etc 和 model.
第 2 步。在 etc 文件夹中,使用以下代码创建 di.xml 文件。
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<preference for="ECMAG\MyReviews\Api\MyReviewInterface"
type="ECMAG\MyReviews\Model\MyReviewClass" />
</config>
接下来,在同一文件夹中创建 module.xml 文件。
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="ECMAG_MyReviews" setup_version="1.0.0"/>
</config>
接下来,在同一文件夹中创建 webapi.xml 文件。
<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd">
<route url="/V1/showreview/allreviews/:storeId" method="GET">
<service class="ECMAG\MyReviews\Api\MyReviewInterface" method="getAllReviews"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>
第 3 步。接下来在 Api 文件夹中创建界面,例如。
<?php
namespace ECMAG\MyReviews\Api;
interface MyReviewInterface
{
/**
* GET review by its ID
*
* @api
* @param string $storeId
* @return array
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getAllReviews($storeId);
}
第 4 步。接下来在模型文件夹中创建 class。
<?php
namespace ECMAG\MyReviews\Model;
use ECMAG\MyReviews\Api\MyReviewInterface;
use Magento\Framework\App\Bootstrap;
class MyReviewClass implements MyReviewInterface{
protected $request;
public function __construct(\Magento\Framework\App\Request\Http $request) {
$this->request = $request;
}
/**
* GET review by its ID
*
* @api
* @param string $storeId
* @return array
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getAllReviews($storeId){
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
$currentStoreId = $storeManager->getStore()->getId();
$rating = $objectManager->get("Magento\Review\Model\ResourceModel\Review\CollectionFactory");
//Apply filter for store id and status='Approved'
$collection = $rating->create()->addStoreFilter($storeId
)->addStatusFilter(\Magento\Review\Model\Review::STATUS_APPROVED);
//Get All parameters from request
$allParameters=$this->request->getParams();
//Check parameter from_Date present or not
if(array_key_exists("fromDate",$allParameters)){
$collection=$collection->addFieldToFilter('created_at', ['gteq' => $allParameters['fromDate']]);
}
//Check parameter to_Date present or not
if(array_key_exists("toDate",$allParameters)){
$collection=$collection->addFieldToFilter('created_at', ['lteq' => $allParameters['toDate']]);
}
//Check parameter title present or not
if(array_key_exists("title",$allParameters)){
$title=$allParameters['title'];
$collection=$collection->addFieldToFilter('title', ['like' => '%'.$title.'%']);
}
//Check parameter text present or not
if(array_key_exists("text",$allParameters)){
$collection=$collection->addFieldToFilter('detail', ['like' => '%'.$allParameters['text'].'%']);
}
//Check parameter customer id present or not
if(array_key_exists("customerId",$allParameters)){
$collection=$collection->addFieldToFilter('customer_id', ['eq' => $allParameters['customerId']]);
}
//Check parameter product id present or not
if(array_key_exists("productId",$allParameters)){
$collection=$collection->addFieldToFilter('entity_pk_value', ['eq' => $allParameters['productId']]);
}
//Check paramter for maximum no. of product per page
if(array_key_exists("pageSize",$allParameters)){
$collection->setPageSize($allParameters['pageSize']);
}
//Check paramter for current page no.
if(array_key_exists("page",$allParameters)){
$collection->setCurPage($allParameters['page']);
}
$result=$collection->getData();
return $result;
}
}
在上述方法中,首先对 Store ID 应用过滤器,然后传入可选参数 URL,如 customer_id、product_id。 参数名称的特殊检查。
第 5 步。最后在 MyReviews 文件夹中创建 registration.php。
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'ECMAG_MyReviews',
__DIR__
);
并调用系统升级命令-> php bin/magento setup:upgrade
在所有上述过程调用 url 之后 -`http://hostname/magento/index.php/rest/V1/showreview/allreviews/1'
在 url 中传递参数时也像- `http://hostname/magento/index.php/rest/V1/showreview/allreviews/1?productId=1'
希望对您有所帮助。