how to use viewmodel instead helper method to acces the system.xml values in phtml in maganto 2

2 Answers 2


Take a look at


When it is good to use viewModel ? What is difference between viewModel and Block?

Create a viewmodel class Test.php at app/code/Vendor/ModuleName/ViewModel and inject Esparksinc\Extension\Helper\Data at it __construct function.

namespace Vendor\ModuleName\ViewModel;

class Test implements \Magento\Framework\View\Element\Block\ArgumentInterface
    private $helperData;
    public function __construct(
        \Esparksinc\Extension\Helper\Data $helperData
        $this->helperData= $helperData;
    public function getSettingValue()
        return $this->helperData>getConfig('Extension/general/enableDisable');

And call this ViewModel class via layout handler

<referenceBlock name="{YourPhtmlFileLayoutName}">
        <argument name="view_model" xsi:type="object">Vendor\ModuleName\ViewModel\Test</argument>

and Call the method of Vendor\ModuleName\ViewModel\Test at your phtml.

<?php /** @var \Vendor\ModuleName\ViewModel\Test $viewModel */ ?>
<?php $viewModel = $block->getData('viewModel') ?>
<?php echo (int) $viewModel->getSettingValue()) ?>
  • how $block->getData is linked because there is no function in block with name of getData(); @Amit
    – usman
    Aug 25, 2019 at 18:24
  • 1
    @usman: i tried $block->getViewModel() in template file and it works :)
    – Guru
    Nov 19, 2019 at 3:30

We all know Helpers from the days of Magento 1. Helpers are classes that provide functionalities for various features throughout the Magento website. Available for use anywhere on the website, Magento 2 Helper can be called in controllers, views, models and even in other helpers too!

Why Create Magento Helpers?

Magento 2 Helpers are usually used as elements that are global and always available. They can even be created as a Singleton (single instances of objects). Helpers can be called everywhere and you only need to inject them in the class. Magento 2 Helpers are mainly created to provide methods for the most common functionalities. For example, helpers are used to build logs in the Magento application.

Magento 2 Helpers In the early versions, Magento 2 had a Helper Factory which allowed developers to instantiate helper methods. ObjectManager was required to instantiate the Helper Factory. So, in this case, you could use the following code:

$object_manager = \Magento\Core\Model\ObjectManager::getInstance(); $helper_factory = $object_manager->get('\Magento\Core\Model\Factory\Helper'); $helper = $helper_factory->get('\Magento\Core\Helper\Data'); If you think there is a problem with the code, you are right!. Fortunately, Magento introduced a better concept: Dependency Injection in Magento 2.

The concept of Dependency Injection states that instead of instantiating an object yourself, the environment will create and provide you the object. For example, if I write a class like this:

class ABC
   public function __contruct(XYZ $xyz)
       $this->xyz= $xyz;

In the constructor of the ABC class, an object of XYZ class is automatically created and assigned the reference, $xyz. This idea is called Dependency Injection.

Through Dependency Injection, Magento 2 provides a high-value concept of loose coupling modules together. When you want to inject into a particular class, you can simply do it by adding an object to its constructor. However, bear in mind that in Magento 2, one dependency is not injected twice.

Magento 1 Helpers In Magento 1, if I want to call a Helper class Data, I use the following statement:

$helper = Mage::helper('core/data'); Given the popularity of the idea, helpers are also available in Magento 2. However, because of the absence of the Mage class, the above statement will produce an error in Magento 2.

Create and Use Magento 2 Helper First, create a simple module in Magento 2. I assume that you already have one.

After creating the module, create a Helper folder in the module directory. Next, create Data.php in the Helper folder. The module’s directory structure will look like:

|-------- Module_Name/
|---------------- composer.json
|---------------- registration.php
|---------------- etc/
|------------------------ module.xml
|---------------- Helper/
|------------------------ Data.php

Add the following code to the newly created Data.php file:

namespace Module_Namespace\Module_Name\Helper;
use \Magento\Framework\App\Helper\AbstractHelper;
class Data extends AbstractHelper
       public function RandomFunc()
               echo "This is Helper in Magento 2";

The helper has been created successfully. In the above Data.php, you can see that I have created a function RandomFunc() that can be called anywhere in Magento 2 using Dependency Injection. To use this Magento 2 helper, follow the below code schema:

use \Module_Namespace\Module_Name\Helper\Data;
class RandomClass
       public function __construct(Data $helper)
               $this->helper = $helper;
       public function func()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.