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
  • What is issue when you are above code? – Nikhil Vaghela Aug 28 '17 at 6:49
  • Provide your issue with your question. – Prince Patel Aug 28 '17 at 6:57
  • after injection has been completed please use di:compile and clear cache and full page cache – Nagaraju K Aug 28 '17 at 7:01
  • Clear var/generation folder – Priyank Aug 28 '17 at 7:05
  • execute php bin/magento setup:update – Mohamed El Mrabet Nov 22 '17 at 21:01
4

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

Hope this helps.

| improve this answer | |
  • Yes. Clearing generation + cache will solve the issue. – Mike Aug 28 '17 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...

| improve this answer | |
  • 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 :) – Purushotam Sharma Aug 28 '17 at 7:02
  • @PurushotamSharma Post your full Constructor – Rajkumar .E Aug 28 '17 at 7:08
  • this is all that I used in my controller file – Purushotam Sharma Aug 28 '17 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);`` – Rajkumar .E Aug 28 '17 at 7:15
  • So what is the right code in the above scenario? – Purushotam Sharma Aug 28 '17 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
| improve this answer | |
  • Wow, Magento 2 is so awkward compared to M1. You have to call 4 commands only for adding a helper. Ridiculous... – Black Sep 17 '19 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. – Shoaib Munir Sep 17 '19 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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