4

I have a module and I am trying to get my module config data ( from system.xml ) within my module controller. It works when I get it in a template file but not when I try to access it in the controler I get this error

Fatal error: Call to undefined method Modzinc\Productpdf\Controller\Index\Index\Interceptor::helper() in ..... public_html/modzinc/app/code/Modzinc/Productpdf/Controller/Index/Index.php on line 18

Here is my controller file located at Modzinc/Productpdf/Controller/Index.php

<?php

namespace Modzinc\Productpdf\Controller\Index;
use FPDF;
use Modzinc\Productpdf\Helper\Data;
use Magento\Framework\App\Action\Action;
use Magento\Framework\Controller\ResultFactory;


class Index extends Action
{
    public function execute()
    {
        ### Settings from Mod Config

        $topTel = $this->helper('Modzinc\Productpdf\Helper\Data')->getConfig('dynamicpdf/general/topTel');

        $pdfFont = 'Arial';

        $product_id=$_GET['id'];
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id);

        $pdf=new FPDF();
        $pdf->AliasNbPages();

        $pdf->SetTitle($product->getName());
        $pdf->AddPage();
        $pdf->SetFont($pdfFont,'',12);

        $pdf->Cell(80);
        $pdf->Cell(210,0,$topTel,1,2,'C');

        $pdf->Output();
    }
}

And my helper file contains the following

<?php

namespace Modzinc\Productpdf\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    public function getConfig($config_path)
    {
        return $this->scopeConfig->getValue(
        $config_path,
        \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }
}

Everything works fine apart from when I try to use the helper to get the config value.

Can someone tell me what I am doing wrong please, I am rather new to Magento 2

1
  • please let me know if you have any issue May 15 '17 at 11:35
9

You need to initiate your helper class in the constructor

protected $_helper;

public function __construct(
   \Magento\Framework\App\Action\Context $context,
   \Modzinc\Productpdf\Helper\Data $helper
) {
     parent::__construct($context);
     $this->_helper = $helper
}

Your final Controller should look like this:

<?php

namespace Modzinc\Productpdf\Controller\Index;
use FPDF;
use Magento\Framework\App\Action\Action;
use Magento\Framework\Controller\ResultFactory;


class Index extends Action
{
    protected $_helper;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Modzinc\Productpdf\Helper\Data $helper
    ) {
        $this->_helper = $helper;
        parent::__construct($context);
    }

    public function execute()
    {
        ### Settings from Mod Config

        $topTel = $this->_helper->getConfig('dynamicpdf/general/topTel');

        $pdfFont = 'Arial';

        $product_id=$_GET['id'];
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id);

        $pdf=new FPDF();
        $pdf->AliasNbPages();

        $pdf->SetTitle($product->getName());
        $pdf->AddPage();
        $pdf->SetFont($pdfFont,'',12);

        $pdf->Cell(80);
        $pdf->Cell(210,0,$topTel,1,2,'C');

        $pdf->Output();
    }
}

Remove var/generation/ folder after adding a new class in the constructor.

rm -rf var/generation/*

To call your helper in a .phtml file

$this->helper("Vendor\Module\Helper\Data");
5
  • Thankyou So Much Prince, You are a Legend, I am proper struggling with this magento 2, thanks again May 15 '17 at 12:03
  • Does not work, as soon as I add my helper to the existing constructor I get Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object
    – Black
    Sep 17 '19 at 10:54
  • @Black Please run compilation command php bin/magento s:d:c Sep 18 '19 at 6:55
  • @PrincePatel, do I have to run this command everytime after chaning a line of code?
    – Black
    Sep 18 '19 at 7:35
  • No, only when you inject class in the constructor Sep 18 '19 at 8:27
2

You have to use construct() method for use helper in your controller,

 public function __construct(
            \Magento\Framework\App\Action\Context $context,
            \Modzinc\Productpdf\Helper\Data $helper
        ) {
            parent::__construct($context);
            $this->helper = $helper
        }

use as below way,

   $this->helper->getConfig('dynamicpdf/general/topTel');

Remove var/generation folder from root.

1
  • Thankyou So Much Rakesh, appreciated your help May 15 '17 at 12:03

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.