Magento 2 Call Helper method in phtml

This tutorial is about Magento 2 Helper method in phtml.i.e Helper method in magento 2. First of all i assume you are familiar with magento 2. And if you are not familiar with advance magento concepts and are looking for basics,please look for magento 2 basic tutorials.

lets start our tutorial.Suppose you want to call a Helper method in template(.phtml) file.How can you do it?. We will discuss it briefly in this tutorial.

It is not a good practice to use helper calls directly in the template.Because it introduces an undeclared dependency. If you want to do this properly you should only call in the template only methods from the block that renders it.Rather you can have your helper instance provided as a dependency to the block that renders the template and create a method in your block that calls the helper and call that method in the template.

You can defined your block like:

protected $helperData;
public function __construct(
     ....
    \QaisarSatti\HelloWorld\Helper\Data $helperData,
    ....
) {
    ....
    $this->helperData = $helperData;
    ....
}

public function doSomething()
{
    return $this->helperData->doSomething();
}

Finally you can call this block like:

$block->doSomething()

Where block something in my case is just for understanding.You will use your own function name.

Similarly there is another way of doing it.Lets have a look at the code:

$helper = $this->helper('QaisarSatti\HelloWorld\Helper\Data');
$values = $helper->YourHelperMethod();

Furthermore you have to write the whole class name in helper as below:

$this->helper('QaisarSatti\HelloWorld\Helper\Data')

You can use it in phtml file using above code.

Further more with some modifications, there is another approach of doing it.

Lets have a look at it.

Likewise you can look at the directory,in which this code is implemented:

/var/www/html/magento2/app/code/QaisarSatti/HelloWorld/Block/Test.php

  <?php
namespace QaisarSatti\HelloWorld\Block;

class Test extends \Magento\Framework\View\Element\Template
{

protected $_helper;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    array $data = [],
    \QaisarSatti\HelloWorld\Helper\Data $helper
) {
    parent::__construct($context, $data);

    $this->_helper = $helper;
}

public function getEnable(){
        return $this->_helper->getEnable();
    }

}

Similarly in

/var/www/html/magento2/app/code/QaisarSatti/HelloWorld/view/frontend/templates/homehorizontalwidget.phtml

you can modify:

 <?php  echo $block->getEnable(); ?>

Furthermore in

/var/www/html/magento2/app/code/QaisarSatti/HelloWorld/Helper/Data.php

. we can use below code:

<?php
namespace QaisarSatti\HelloWorld\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper {

    /** * @var \Magento\Framework\App\Config\ScopeConfigInterfac
        */

    protected $_scopeConfig;
    CONST ENABLE = 'helloworld/general/enable_module';


    public function __construct( \Magento\Framework\App\Helper\Context $context,
            \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig ) {

             parent::__construct($context); $this->_scopeConfig = $scopeConfig;
    }

    public function getEnable(){
        return $this->_scopeConfig->getValue(self::ENABLE);
    }

}

Similarly in

/var/www/html/magento2/app/code/QaisarSatti/HelloWorld/etc/adminhtml/system.xml

System configurations are created.

Qaisar Satti

Hi, I'm Qaisar Satti! I've been a developer for over 20 years, and now I love sharing what I've learned through tutorials and guides. Whether you're working with Magento, PrestaShop, or WooCommerce, my goal is to make your development journey a bit easier and more fun. When I'm not coding or writing, you can find me exploring new tech trends and hanging out with the amazing developer community. Thanks for stopping by, and happy coding!

Leave a Reply