Magento 2 Controller – Loading Helper

helpermagento-2.1magento2magento2.2

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

Best Answer

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");
Related Topic