3

I am not sure its happening with me because i am a newbie in magento2. Every time i try to override a constructor i end with an error. Tried to find out a lot but nothing helps. So at last decided to post here.

I want to override the execute method of Magento\Catalog\Controller\Category\View file. Basically category view page.

What i am doing is

namespace namespace\module\Controller\Magento\Catalog\Category;

class View extends \Magento\Catalog\Controller\Category\View
{
    protected $_coreRegistry = null;
    protected $_catalogSession;
    protected $_catalogDesign;
    protected $_storeManager;
    protected $categoryUrlPathGenerator;
    protected $resultPageFactory;
    protected $resultForwardFactory;
    private $layerResolver;
    protected $categoryRepository;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Catalog\Model\Design $catalogDesign,
        \Magento\Catalog\Model\Session $catalogSession,
        \Magento\Framework\Registry $coreRegistry,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator,
        PageFactory $resultPageFactory,
        \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory,
        Resolver $layerResolver,
        CategoryRepositoryInterface $categoryRepository
    ) {
        $this->_storeManager = $storeManager;
        $this->_catalogDesign = $catalogDesign;
        $this->_catalogSession = $catalogSession;
        $this->_coreRegistry = $coreRegistry;
        $this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
        $this->resultPageFactory = $resultPageFactory;
        $this->resultForwardFactory = $resultForwardFactory;
        $this->layerResolver = $layerResolver;
        $this->categoryRepository = $categoryRepository;
        parent::__construct($context,$storeManager,$catalogDesign,$coreRegistry,$categoryUrlPathGenerator,$resultPageFactory,$resultForwardFactory,$layerResolver,$categoryRepository);
    }

    public function execute()
    {
       // DO your code here.
    }

}

Now i am getting an error. Which says

Fatal error: Uncaught TypeError: Argument 7 passed to namespace\module\Controller\Magento\Catalog\Category\View::__construct() must be an instance of namespace\module\Controller\Magento\Catalog\Category\PageFactory, instance of Magento\Framework\View\Result\PageFactory given

I think this happens each time if its a factory class. Not sure though. Do you guys have any idea why it shows. Any help will be appreciated.

1 Answer 1

7

Try following way:

namespace Vendor\Module\Controller\Category;

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\Framework\View\Result\PageFactory;

class View extends \Magento\Catalog\Controller\Category\View
{
    /**
     * @var \Magento\Customer\Model\CustomerFactory
     */
    protected $customerFactory;

    /**
     * Constructor
     *
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Catalog\Model\Design $catalogDesign
     * @param \Magento\Catalog\Model\Session $catalogSession
     * @param \Magento\Framework\Registry $coreRegistry
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     * @param \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory
     * @param Resolver $layerResolver
     * @param CategoryRepositoryInterface $categoryRepository
     * @param \Magento\Customer\Model\CustomerFactory $customerFactory
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Catalog\Model\Design $catalogDesign,
        \Magento\Catalog\Model\Session $catalogSession,
        \Magento\Framework\Registry $coreRegistry,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator,
        PageFactory $resultPageFactory,
        \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory,
        Resolver $layerResolver,
        CategoryRepositoryInterface $categoryRepository,
        \Magento\Customer\Model\CustomerFactory $customerFactory
    ) {

        $this->customerFactory = $customerFactory;

        parent::__construct(
            $context,
            $catalogDesign,
            $catalogSession,
            $coreRegistry,
            $storeManager,
            $categoryUrlPathGenerator,
            $resultPageFactory,
            $resultForwardFactory,
            $layerResolver,
            $categoryRepository
        );
    }

    public function execute()
    {
        die('hello');
    }
}

Delete var/generation/*

N.B: Always try to use plugin, instead of overwrite whole class.

8
  • Do you have any idea whenever i try to inject $this->customerFactory = $customerFactory; i get an error. Like Magento\Customer\Model has this. But thats not working here. Jan 19, 2017 at 19:24
  • I am getting this. Class namespace\module\Model\Customer\CustomerFactory does not exist . Question is it should take magento's customer factory. But the error says my module's customer factory doesnt exists. Why so. Jan 19, 2017 at 19:26
  • Check updated answer.
    – Sohel Rana
    Jan 19, 2017 at 19:33
  • Hi Sohel as seems you are knowledge enough can you also please tell me if there are any kind of order needs to be maintained in constructor?...I mean if i need to add something new then i will add it at the end. But what if i want to override any function from Magento\Customer\Model\AccountManagement and then it shows Class namespace\module\Model\Customer\CustomerFactory does not exist And i add like you said in the begining of the constructor because originally its in the beginning. That will work too? Jan 19, 2017 at 19:47

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.