8

I am getting "Uncaught ArgumentCountError: Too few arguments to function on parent::__construct($context);

Index.php

<?php

namespace MyTheme\Theme\Controller\Index;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Index extends \Magento\Framework\App\Action\Action
{   
    /**
    * @var \Magento\Framework\View\Result\PageFactory
    */
    protected $resultPageFactory;

    public function __construct(Context $context, PageFactory $pageFactory)
    {
        parent::__construct($context);
        $this->resultPageFactory = $pageFactory;
    }

    public function execute()
    {
        $result = $this->resultPageFactory->create();
        return $result;
    }
}

Even if I use parent::__construct($context, $pageFactory); instead of parent::__construct($context); I get the same error.

6
  • Should remove the '\'. Oct 26, 2017 at 13:19
  • @KhoaTruongDinh from where?
    – brazuka
    Oct 26, 2017 at 13:30
  • Remove the '\' from use after namespace Oct 26, 2017 at 13:32
  • @SureshChikani I remove the '\' but I still get the same error. I just updated the code.
    – brazuka
    Oct 26, 2017 at 13:36
  • 2
    Just run one time upgrade command it will be solved Sep 20, 2018 at 5:05

3 Answers 3

12

After a couple hours I found a solution. I had to run the commands below:

  1. php bin/magento cache:flush
  2. php bin/magento cache:clean
  3. php bin/magento setup:upgrade
  4. bin/magento setup:di:compile
  5. From magento root directory: chmod -R 777 var/*
1
  • 3
    Why would like to make each file in var/ executable? Better use find -type d chmod 777 {} \;
    – Alex
    Jan 8, 2019 at 13:23
10

It means that a old function signature is still cached. If you are during development,

php bin/magento deploy:mode:set developer

might be enough. Otherwise brazuka's answer would help.

1

Follow the following steps:

1. From magento root directory: chmod -R 777 var/ generated/

2. From magento root directory: rm -rf generated/code/MyTheme

Reason:

There might be already generated Interceptor class, then you pass additional dependency injection to the constructor.The interceptor class is not rewrite properly.

Your Answer

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

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