리뷰 모델 용 컨트롤러를 다시 작성했습니다. composer.json 파일 :
{
"name": "apple/module-review",
"description": "N/A",
"require": {
"php": "~5.5.0|~5.6.0|~7.0.0",
"magento/framework": "100.0.*"
},
"type": "magento2-module",
"version": "100.0.2",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Apple\\Review\\": ""
}
}
}
registration.php 파일
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Apple_Review',
__DIR__
);
app / code / Apple / Review / etc / module.xml 파일 :
app/code/Apple/Review/etc/di.xml file for override review controller.
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Review\Controller\Product\Post" type="Apple\Review\Controller\Post" />
</config>
검토 모델의 컨트롤러 파일에서
app / code / Apple / Review / Controller / Post.php
use Magento\Review\Controller\Product as ProductController;
use Magento\Framework\Controller\ResultFactory;
use Magento\Review\Model\Review;
class Post extends \Magento\Review\Controller\Product\Post
{
public function execute()
{
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
if (!$this->formKeyValidator->validate($this->getRequest())) {
$resultRedirect->setUrl($this->_redirect->getRefererUrl());
return $resultRedirect;
}
$data = $this->reviewSession->getFormData(true);
if ($data) {
$rating = [];
if (isset($data['ratings']) && is_array($data['ratings'])) {
$rating = $data['ratings'];
}
} else {
$data = $this->getRequest()->getPostValue();
$rating = $this->getRequest()->getParam('ratings', []);
}
if (($product = $this->initProduct()) && !empty($data)) {
/** @var \Magento\Review\Model\Review $review */
$review = $this->reviewFactory->create()->setData($data);
$validate = $review->validate();
if ($validate === true) {
try {
$review->setEntityId($review->getEntityIdByCode(Review::ENTITY_PRODUCT_CODE))
->setEntityPkValue($product->getId())
->setStatusId(Review::STATUS_PENDING)
->setCustomerId($this->customerSession->getCustomerId())
->setStoreId($this->storeManager->getStore()->getId())
->setStores([$this->storeManager->getStore()->getId()])
->save();
foreach ($rating as $ratingId => $optionId) {
$this->ratingFactory->create()
->setRatingId($ratingId)
->setReviewId($review->getId())
->setCustomerId($this->customerSession->getCustomerId())
->addOptionVote($optionId, $product->getId());
}
$review->aggregate();
$this->messageManager->addSuccess(__('You submitted your review for moderation.Thanks'));
} catch (\Exception $e) {
$this->reviewSession->setFormData($data);
$this->messageManager->addError(__('We can\'t post your review right now.'));
}
} else {
$this->reviewSession->setFormData($data);
if (is_array($validate)) {
foreach ($validate as $errorMessage) {
$this->messageManager->addError($errorMessage);
}
} else {
$this->messageManager->addError(__('We can\'t post your review right now.'));
}
}
}
$redirectUrl = $this->reviewSession->getRedirectUrl(true);
$resultRedirect->setUrl($redirectUrl ?: $this->_redirect->getRedirectUrl());
return $resultRedirect;
}
}
이것은 magento2의 검토 컨트롤러 재정의를위한 작동 코드입니다. 감사.