1

I get the __construct() argument error while trying to override 2 custom modules from same core class as:

In etc/di - Module1

<preference for="Magento\CatalogSearch\Controller\Result\Index" type="Vendor\Module1\Controller\Result\Index" />

in Controller class

class Index extends \Magento\CatalogSearch\Controller\Result\Index
{
    public function __construct(
        Context $context,
        Session $catalogSession,
        StoreManagerInterface $storeManager,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        Resolver $layerResolver,
        QueryFactory $queryFactory
    ) {
        parent::__construct($context,$catalogSession,$storeManager,$resultPageFactory,$queryFactory,$layerResolver);
        $this->resultPageFactory = $resultPageFactory;
        $this->layerResolver = $layerResolver;
        $this->_queryFactory = $queryFactory;
        $this->_storeManager = $storeManager;
    }

in etc/di--module2

<preference for="Vendor\Module1\Controller\Result\Index" type="Vendor\Module2\Controller\Result\Index" />

in controller module2

class Index extends \Vendor\Module1\Controller\Result\Index
{
    public function __construct(
        Context $context,
        Session $catalogSession,
        StoreManagerInterface $storeManager,
        Http $request,
        LayoutInterface $layout,
        ScopeConfigInterface $scopeConfig,
        Resolver $layerResolver,
        \Magento\Catalog\Model\CategoryFactory $catalogCategoryFactory,
        \Magento\Framework\Registry $registry,
        \Vendor\Module1\Model\Client\Connector $tglssearchClientConnector,
        QueryFactory $queryFactory,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        parent::__construct($context,$catalogSession,$storeManager,$queryFactory,$layerResolver);
        $this->_storeManager = $storeManager;
        $this->catalogSession = $catalogSession;
        $this->storeManager = $storeManager;
        $this->request = $request;
        $this->layout = $layout;
        $this->scopeConfig = $scopeConfig;
        $this->catalogCategoryFactory = $catalogCategoryFactory;
        $this->registry = $registry;
        $this->tglssearchClientConnector = $tglssearchClientConnector;
        $this->layerResolver = $layerResolver;
        $this->_queryFactory = $queryFactory;
        $this->resultPageFactory = $resultPageFactory;

        /* parent::__construct(
            $context
        ); */
    }

I get:

Argument 4 passed to Vendor\Module1\Controller\Result\Index::__construct() must be an instance of Magento\Framework\View\Result\PageFactory, instance of Magento\Search\Model\QueryFactory given, called in C:\xampp\htdocs\magento2x_5test\app\code\Vendor\Module2\Controller\Result\Index.php on line 36 and defined in C:\xampp\htdocs\magento2x_5test\app\code\Vendor\Module1\Controller\Result\Index.php on line 22

2
  • Try to clean the var/generation folder Sep 19, 2016 at 11:21
  • its cleared...var/gen and var/cache
    – Sushivam
    Sep 19, 2016 at 11:25

2 Answers 2

2

In controller module2 try to change:

parent::__construct($context,$catalogSession,$storeManager,$queryFactory,$layerResolver);

to the:

parent::__construct($context,$catalogSession,$storeManager,$resultPageFactory,$layerResolver,$queryFactory);

because you module1 controller constructor get this params in this order:

public function __construct(
    Context $context,
    Session $catalogSession,
    StoreManagerInterface $storeManager,
    \Magento\Framework\View\Result\PageFactory $resultPageFactory,
    Resolver $layerResolver,
    QueryFactory $queryFactory
) { ... }

Update:

In module 1 controller class change the line:

parent::__construct($context,$catalogSession,$storeManager,$resultPageFactory,$queryFactory,$layerResolver);

to the:

parent::__construct($context, $catalogSession, $storeManager, $queryFactory, $layerResolver);

Note:
clean the var/generation folder to see the changes.

2
  • After changing, Argument 4 passed to Magento\CatalogSearch\Controller\Result\Index::__construct() must be an instance of Magento\Search\Model\QueryFactory, instance of Magento\Framework\View\Result\PageFactory given, called in C:\xampp\htdocs\magento2x_5test\app\code\Vendor\Module1\Controller\Result\Index.php on line 26 and defined in C:\xampp\htdocs\magento2x_5test\app\code\Magento\CatalogSearch\Controller\Result\Index.php on line 53
    – Sushivam
    Sep 19, 2016 at 11:33
  • @SachinS I have updated the answer Sep 19, 2016 at 11:41
1

make the constructor of your first controller class look like this:

class Index extends \Magento\CatalogSearch\Controller\Result\Index
{
public function __construct(
    Context $context,
    Session $catalogSession,
    StoreManagerInterface $storeManager,
    Resolver $layerResolver,
    QueryFactory $queryFactory,
    \Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
    parent::__construct($context,$catalogSession,$storeManager,$queryFactory,$layerResolver);
    $this->resultPageFactory = $resultPageFactory;
}

and remove the constructor from the second class you posted.

2
  • 1st constructor as said, and 2nd i kept public function __construct(Http $request) {$this->request = $request;} ...Call to a member function dispatch() on null...cleared gen and cache
    – Sushivam
    Sep 19, 2016 at 11:52
  • Ok thanks guys, fixed it...Class order was the problem..
    – Sushivam
    Sep 19, 2016 at 12:12

Your Answer

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

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