Magento 2 – Source Class Result for ResultFactory Generation Does Not Exist

dimagento-2.1magento2

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.

Best Answer

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.

Related Topic