0

How to call helper function in controller:-

I have use below code:-

class Index extends \Magento\Framework\App\Action\Action
{
    protected $helperData;

    public function __construct(
         ....
        \{Vendor}\{Module}\Helper\Data $helperData,
        ....
    ) {
        ....
        $this->helperData = $helperData;
        ....
    }       

    public function execute()
    {
        $this->helperData-><functionName>();
        return;     
    }
}

this code give me error as below:-

Uncaught TypeError: Argument 2 passed to {Vendor}\{Module}\Controller\Callforprice\Index::__construct() must be an instance of {Vendor}\{Module}\Helper\Data, none given, called in
5
  • What is issue when you are above code? Commented Aug 28, 2017 at 6:49
  • Provide your issue with your question. Commented Aug 28, 2017 at 6:57
  • after injection has been completed please use di:compile and clear cache and full page cache Commented Aug 28, 2017 at 7:01
  • Clear var/generation folder
    – Priyank
    Commented Aug 28, 2017 at 7:05
  • execute php bin/magento setup:update Commented Nov 22, 2017 at 21:01

3 Answers 3

4

Code is correct. This happened to me many times. I have cleared /var/generation directory when altering constructor parameters.

Hope this helps.

1
  • Yes. Clearing generation + cache will solve the issue.
    – Mike
    Commented Aug 28, 2017 at 8:14
0

You can try below code..

 class Index extends \Magento\Framework\App\Action\Action
  {

protected $helperData;

public function __construct(
     \Magento\Framework\App\Action\Context $context,
    \{Vendor}\{Module}\Helper\Data $helperData

) {

    $this->helperData = $helperData;
    parent::__construct($context, $helperData);

}

public function execute()
{
    $this->helperData-><functionName>();
    return;     
}
}

Then, Clear Cache and try it, let me know if you have issue...

6
  • Sorry during the draft my question I have forget to add the the construct and function into class, So i have updated my question, thanks for your answer anyway, good observation :) Commented Aug 28, 2017 at 7:02
  • @PurushotamSharma Post your full Constructor Commented Aug 28, 2017 at 7:08
  • this is all that I used in my controller file Commented Aug 28, 2017 at 7:10
  • With In this class {Vendor}\{Module}\Controller\Callforprice\Index You are passing multiple arguments in ` parent::__construct($context, $second, $thrid);` If second argument needed \{Vendor}\{Module}\Helper\Data instance, you need to pass `parent::__construct($context, $helperData ,$second, $thrid);`` Commented Aug 28, 2017 at 7:15
  • So what is the right code in the above scenario? Commented Aug 28, 2017 at 7:24
0

These types of errors (Uncaught TypeError: Argument 2 passed to...) occures when you add more parameters to constructor, it would fix after you run di compile command to regenerate your code in generated directory, run following command and it will fix.

php -dmemory_limit=1G bin/magento setup:upgrade
php -dmemory_limit=1G bin/magento setup:di:compile
php -dmemory_limit=1G bin/magento setup:static-content:deploy -f
php bin/magento cache:clean
2
  • Wow, Magento 2 is so awkward compared to M1. You have to call 4 commands only for adding a helper. Ridiculous...
    – Black
    Commented Sep 17, 2019 at 11:01
  • LOL. That's true, but I am used to it now. And I find Magento 2 more better than 1.9 in many ways. If you are new to M2 then I must say you do not judge it now. It least work on 2 or 3 projects first. Commented Sep 17, 2019 at 11:09

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.