0

I am trying to override the controller of an extension in Magento2.

My di.xml file is:-

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
      <preference for="Mycustom\Custom\Controller\Adminhtml\Controllerfolder\Fileaction" type="ActualModule\Custom\Controller\Adminhtml\Controllerfolder\Fileaction"/>

</config>

I have cloned the controller file with updated namespaces, and extend the controller to module's file like:-

<?php

namespace Mycustom\Custom\Controller\Adminhtml\Controllerfolder;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;

use ActualModule\Custom\Model\OrderFactory;

class Fileaction extends \ActualModule\Custom\Controller\Adminhtml\Controllerfolder\Fileaction
{
    /**
     * @var OrderFactory
     */
    protected $_modelOrderFactory;

    public function __construct(Context $context, Filter $filter, CollectionFactory $collectionFactory, 
        OrderFactory $modelOrderFactory)
    {
        parent::__construct($context, $filter);
        $this->collectionFactory = $collectionFactory;
        $this->_modelOrderFactory = $modelOrderFactory;
    }
    protected  function massAction(AbstractCollection $collection)
{

    die('here');
 }

It should be functional but the issue I get on php bin/magento s:d:c is as follow:-

Errors during compilation:

    ActualModule\Custom\Controller\Adminhtml\Controllerfolder\Fileaction
                Missed required argument collectionFactory in parent::__construc
t call. File: C:/wamp64/www/magento/app/code/Mycustom\Custom\Controller\Adminhtml\Controllerfolder/Fileaction.php
Total Errors Count: 1
Errors during compilation:

I feel like I missed something but not able to catch what.

Just to add var/generation, di etc.. clearing has been done, cache flushed.

1

Repalce your controller constructer like this.

public function __construct(Context $context, Filter $filter, CollectionFactory $collectionFactory, 
        OrderFactory $modelOrderFactory)
    {
        parent::__construct($context, $filter,$collectionFactory,$modelOrderFactory);
        $this->collectionFactory = $collectionFactory;
        $this->_modelOrderFactory = $modelOrderFactory;
    }
  • The code seems to work, but I am just checking that it overrides too. Also I request you to please explain a bit why the same function did not gave any error when it is being used in module's controller, but gave error in my module. I have understood the issue in my code though, but just curious to know in more depth. Thanks in advance – Gagan Jan 24 '18 at 13:45
  • If my answer help you, please accept it. So it will help other in community. – Dhiren Vasoya Jan 24 '18 at 13:47
  • For the magento2 overiding concept, you need to pass all required pararameter to parent controller. – Dhiren Vasoya Jan 24 '18 at 13:48
  • Actually it did not override the controller, but resolved the s:d:c issue. – Gagan Jan 24 '18 at 13:48
  • 1
    If my answer help you, please accept it. So it will help other in community. – Dhiren Vasoya Jan 24 '18 at 13:49

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.