Advertisements

Today we use Helper in are as follow :

  • Create Helper

  • Use Helper in Controller

  • Use Helper in Block

  • Use Helper in phtml

how to use helper in Magento 2

Helper is something that is ever ready to help you in any piece of code that provides you with a reusable function or snippet.

Like mail function, getting image URL for products, and many more.

So in this post, I want you to focus on “$this->_dataHelper" and if you figure out how it gets used then the helper is your best friend for global uses.

So In this post, I am creating a helper with the basic snippet and then on the controller I have to use the exit function in it to show you that helper is working while visiting to frontend URL, then we will look at how to use it on Block and last one in phtml file.

Path : app/code/Nilesh/Helpers/Helper/Data.php
<?php

declare(strict_types=1);

namespace Nilesh\Helpers\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

class Data extends AbstractHelper
{

    public function __construct(
        \Magento\Framework\App\Helper\Context $context
    ) {
        parent::__construct($context);
    }

    public function printHello()
    {
        return "Hello";
    }
}

Path : app/code/Nilesh/Helpers/Controller/Index/Index.php
<?php

declare(strict_types=1);

namespace Nilesh\Helpers\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{

    protected $resultPageFactory;
    protected $_dataHelper;

    public function __construct(
        \Nilesh\Helpers\Helper\Data $dataHelper,
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        $this->_dataHelper = $dataHelper;
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        echo $this->_dataHelper->printHello();
        exit();
    }
}

Path : app/code/Nilesh/Helpers/Block/Index/Index.php
<?php

declare(strict_types=1);

namespace Nilesh\Helpers\Block\Index;

class Index extends \Magento\Framework\View\Element\Template
{
    protected $_dataHelper;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Nilesh\Helpers\Helper\Data $dataHelper,
        array $data = []
    ) {
        $this->_dataHelper = $dataHelper;
        parent::__construct($context, $data);
    }

    public function getHelperHelloPrint()
    {
        return $this->_dataHelper->printHello();
    }
}

Path : app/code/Nilesh/Helpers/view/frontend/templates/index/index.phtml
<?php 
    $dataHelper = $this->helper(Nilesh\Helpers\Helper\Data::class);
    echo $dataHelper->printHello(); 
?>

Command : php bin/magento setup:upgrade
Command: php bin/magento setup:static-content:deploy -f
Command : php bin/magento c:f

This Code “how to use helper in magento 2" is all for demonstration and Idea how we can use it.

And also we can use a helper in many more way this all is basic and regularly used

Happy coding and Thanks

Please comment below whatever every comes in your mind weather it is question, anger, suggestion, improvement. Please write down below. I want to hear you and if you want me to make a post on something or wanting snippet please that also you can mention in comment.

I will surely answer your comment not letting go unanswered or help you in your query.

You all are the semicolon to my statements; Please support me; Thanks