Loading Helper in Controller
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");
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.