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.
-
Can you post the full error message. You only posted partially.André Ferraz– André Ferraz2018年02月13日 16:36:58 +00:00Commented Feb 13, 2018 at 16:36
1 Answer 1
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
-
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 $customerDavid Lightman– David Lightman2018年02月14日 08:22:42 +00:00Commented Feb 14, 2018 at 8:22