0

I am creating a custom module,

at the end of which I am redirecting it to checkout cart url.

Here is my code, of the index class of custom module.

<?php

namespace Neo\Pincode\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $_logger;

    protected $_responseFactory;

    protected $_messageManager;

    protected $_scopeConfig;

    protected $_urlInterface;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,// Context injection
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, // ScopeConfig injection
        \Psr\Log\LoggerInterface $logger //log injection
    ) {
        $this->_resultFactory = $context->getResultFactory();
        $this->_scopeConfig = $scopeConfig;
        $this->_logger = $logger;
        $this->_messageManager = $context->getMessageManager();
        parent::__construct($context);
    }

    public function execute()
    {
        $isValid = 0;
        $availablePincodes =  $this->_scopeConfig->getValue('pincode_section/pincode_group/pincode_field', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
        $pincodes = explode(",",$availablePincodes);
        $userPincode = $this->getRequest()->getParam('pincode');
        foreach ($pincodes as $key => $value) {
            if($value==$userPincode){
                $isValid==1;
                break;
            }
        }
        if($isValid==1){
            $this->_messageManager->addSuccess("Delivery available for above area");
        }
        else{
            $this->_messageManager->addError("Delivery not available for above area");
        }

        $resultRedirect = $this->_resultFactory->create(ResultFactory::TYPE_REDIRECT);
        $resultRedirect->setUrl($this->getBaseUrl().'checkout/cart');
        return $resultRedirect;  
    }
}

When this method is excecuted, then at the execution of last line, this error occurs,

Source class "\Neo\Pincode\Controller\Index\Result" for "Neo\Pincode\Controller\Index\ResultFactory" generation does not exist.

I think it is some dependency injection conflict.

Please help, to get out from the issue.

1 Answer 1

4

Add use Magento\Framework\Controller\ResultFactory; after namespace line. You have missing Above class file.

<?php

namespace Neo\Pincode\Controller\Index;

use Magento\Framework\Controller\ResultFactory;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $_logger;

    protected $_responseFactory;

    protected $_messageManager;

    protected $_scopeConfig;

    protected $_urlInterface;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,// Context injection
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, // ScopeConfig injection
        \Psr\Log\LoggerInterface $logger //log injection
    ) {
        $this->_resultFactory = $context->getResultFactory();
        $this->_scopeConfig = $scopeConfig;
        $this->_logger = $logger;
        $this->_messageManager = $context->getMessageManager();
        parent::__construct($context);
    }

    public function execute()
    {
        $isValid = 0;
        $availablePincodes =  $this->_scopeConfig->getValue('pincode_section/pincode_group/pincode_field', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
        $pincodes = explode(",",$availablePincodes);
        $userPincode = $this->getRequest()->getParam('pincode');
        foreach ($pincodes as $key => $value) {
            if($value==$userPincode){
                $isValid==1;
                break;
            }
        }
        if($isValid==1){
            $this->_messageManager->addSuccess("Delivery available for above area");
        }
        else{
            $this->_messageManager->addError("Delivery not available for above area");
        }

        $resultRedirect = $this->_resultFactory->create(ResultFactory::TYPE_REDIRECT);
        $resultRedirect->setUrl($this->getBaseUrl().'checkout/cart');
        return $resultRedirect;  
    }
}

remove var folder and clear cache. Check again.

4
  • Thanks @Rakesh. Error is removed, I will accept in no time Commented Mar 29, 2017 at 6:47
  • But now it is getting error as Invalid return type Commented Mar 29, 2017 at 6:47
  • 1
    try with, $resultRedirect->setPath('checkout/cart'); return $resultRedirect; Commented Mar 29, 2017 at 6:52
  • You're welcome. Commented Mar 29, 2017 at 6:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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