0

I have been struggling with a problem for a while and hope somebody can point me in the right direction.

I am attempting to extend a native controller by adding my own module. This appears to have worked successfully in the most part. I now have a need to call a bespoke model as an additional parameter in my overriding controllers constructor.

When I attempt to add the new parameter, I am met with the following error:

Uncaught ArgumentCountError: Too few arguments to function Companyname\Modulename\Controller\Result\Index::__construct(), 5 passed in 

Here is my code for my working controller as an example:


namespace Companyname\Modulename\Controller\Result;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\Catalog\Model\Session;
use Magento\Framework\App\Action\Context;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Search\Model\QueryFactory;
class Index extends \Magento\CatalogSearch\Controller\Result\Index
{
 protected $layerResolver;
 protected $_queryFactory;
 public function __construct(
 Context $context,
 Session $catalogSession,
 StoreManagerInterface $storeManager,
 QueryFactory $queryFactory,
 Resolver $layerResolver,
 ) {
 parent::__construct(
 $context,
 $catalogSession,
 $storeManager,
 $queryFactory,
 $layerResolver,
 $customer
 );
 $this->layerResolver = $layerResolver;
 $this->_queryFactory = $queryFactory;
 }
 public function execute()
 {
 $this->layerResolver->create(Resolver::CATALOG_LAYER_SEARCH);
 $query = $this->_queryFactory->get();
 // more code
 }
}

Maybe I am going about this is completely the wrong way. if somebody could give me a few pointers I'd appreciate it.

Teja Bhagavan Kollepara
3,8275 gold badges33 silver badges69 bronze badges
asked Feb 13, 2018 at 16:33
1
  • Can you post the full error message. You only posted partially. Commented Feb 13, 2018 at 16:36

1 Answer 1

2

I am not sure what is that $customer in your constructor, but I think you should inject it in your constructor, and leave parent as it is (5 parameters)

public function __construct(
 Context $context,
 Session $catalogSession,
 StoreManagerInterface $storeManager,
 QueryFactory $queryFactory,
 Resolver $layerResolver,
 $customer
) {
 parent::__construct(
 $context,
 $catalogSession,
 $storeManager,
 $queryFactory,
 $layerResolver
 );
 $this->layerResolver = $layerResolver;
 $this->_queryFactory = $queryFactory;
}

Another thing, which I am not totally sure, is the order in the code... maybe you'd need to change this to make it work

public function __construct(
 Context $context,
 Session $catalogSession,
 StoreManagerInterface $storeManager,
 QueryFactory $queryFactory,
 Resolver $layerResolver,
 $customer
) {
 $this->layerResolver = $layerResolver;
 $this->_queryFactory = $queryFactory;
 parent::__construct(
 $context,
 $catalogSession,
 $storeManager,
 $queryFactory,
 $layerResolver
 );
}

But, let me insist we can't know what class is that $customer injected in your constructor. Maybe that's the main problem

answered Feb 13, 2018 at 17:44
1
  • Thanks for your reply. Sorry I had not intended to leave the $customer variable in the constructor. When I tried it before I had this $customer declared as my model class as below but it then gives me an error saying 'too few arguments': Companyname\Modulename\Model\Customer $customer Commented Feb 14, 2018 at 8:22

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.