0

I have modified form review as popup modal and have overrides the controller in my custom module, the review saved successfully in DB. I need to pass the response to the to JS in JSON format , Help me how to solve it. Here I have given my custom controller but it shows error while compiling

Errors during compilation: XXX\YYY\Controller\Index\Index Missed required argument coreRegistry in parent::__construct call. File: /var/www/html/magento220/app/code/XXX/YYY/Controller/Index/Index.php Total Errors Count: 1

                                          [Magento\Framework\Validator\Exception]     Error during compilation

Controller

<?php
namespace XXX\YYY\Controller\Index;


use Magento\Review\Model\Review;
use Magento\Framework\Controller\Result\JsonFactory;

class Index extends \Magento\Review\Controller\Product\Post
{

    protected $_resultJsonFactory;

    public function __construct(
    \Magento\Framework\App\Action\Context $context, JsonFactory $resultJsonFactory
    )
    {
        $this->_resultJsonFactory = $resultJsonFactory;

        parent::__construct($context);
    }

    public function execute()
    {
        $message = null;
        $status = null;
        if (!$this->formKeyValidator->validate($this->getRequest())) {
            $message[] = "Invalid form key";
            $status = 'failed';
        } else {
            $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', []);
            }
            echo 'sdfsdfsdfsdfsd';
            print_r($data);

            if (($product = $this->initProduct()) && !empty($data)) {
                echo 'InsideLoops';
                /** @var \Magento\Review\Model\Review $review */
                $review = $this->reviewFactory->create()->setData($data);

                $review->unsetData('review_id');

                $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());
                        }
                        print_r($optionId);
                        $review->aggregate();
                        $message[] = __('You submitted your review for moderation.');
                        $status = 'success';
                    } catch (\Exception $e) {
                        $this->reviewSession->setFormData($data);
                        $message[] = __('We can\'t post your review right now.');
                        $status = 'failed';
                    }
                } else {
                    $this->reviewSession->setFormData($data);
                    if (is_array($validate)) {
                        foreach ($validate as $errorMessage) {
                            $message[] = $errorMessage;
                        }
                    } else {
                        $message[] = __('We can\'t post your review right now.');
                    }
                    $status = 'failed';
                }
            }
        }

        $resultJson = $this->_resultJsonFactory->create()
                ->setData([
            'messages' => $message,
            'status' => $status,
        ]);


        return $resultJson;

    }
4
  • Is there any need to call the parent __construct method in child __construct method : parent::__construct($context);
    – D Singh
    Dec 20, 2017 at 6:03
  • Yes , Because I need to use json resultfactory to return result to js
    – Jaisa
    Dec 20, 2017 at 6:07
  • you can use json resultfactory without use of parent::__construct($context); | Once comment this code parent::__construct($context) in __construct method and compile the code again and check now.
    – D Singh
    Dec 20, 2017 at 6:09
  • I have solved my error by calling its parent constructor without constructor it shows an error
    – Jaisa
    Dec 20, 2017 at 6:29

1 Answer 1

0

Use this construct method in your controller.

  public function __construct(
    \Magento\Framework\App\Action\Context $context, JsonFactory $resultJsonFactory
    )
    {
        $this->_resultJsonFactory = $resultJsonFactory;

    }
3
  • Nope, It shows same error
    – Jaisa
    Dec 20, 2017 at 6:16
  • Have you hit this command : php bin/magento setup:di:compile
    – D Singh
    Dec 20, 2017 at 6:24
  • yes, even after it shows same error
    – Jaisa
    Dec 20, 2017 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.