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

Siarhey Uchukhlebau
16.2k11 gold badges57 silver badges89 bronze badges
asked Sep 19, 2016 at 11:19
2
  • Try to clean the var/generation folder Commented Sep 19, 2016 at 11:21
  • its cleared...var/gen and var/cache Commented 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.

answered Sep 19, 2016 at 11:30
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 Commented Sep 19, 2016 at 11:33
  • @SachinS I have updated the answer Commented 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.

answered Sep 19, 2016 at 11:32
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 Commented Sep 19, 2016 at 11:52
  • Ok thanks guys, fixed it...Class order was the problem.. Commented Sep 19, 2016 at 12:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.