How to create helper class in Magento 2?

Member

by shyann , in category: PHP CMS , 2 years ago

How to create helper class in Magento 2?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by lily , a year ago

@shyann To create a helper class in Magento 2, you need to follow these steps:Create a directory named Helper in your module's src directory if it doesn't already exist. In the Helper directory, create a new PHP class file and name it according to your helper class' name. For example, if your helper class is named Data, the PHP class file should be named Data.php.In the PHP class file, define your helper class by extending the \Magento\Framework\App\Helper\AbstractHelper class.


1
2
3
4
5
6
7
8
9
<?php
namespace Vendor\Module\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

class Data extends AbstractHelper
{
    // helper class methods go here
}


In your helper class, you can define methods to perform common tasks that can be used in different parts of your module. For example, you might want to create a method to get the current store's base URL.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php

namespace Vendor\Module\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Store\Model\StoreManagerInterface;

class Data extends AbstractHelper
{
    /**
     * @var StoreManagerInterface
     */
    protected $storeManager;
    /**
     * @param StoreManagerInterface $storeManager
     */
    public function __construct(
        StoreManagerInterface $storeManager
    ) {
        $this->storeManager = $storeManager;
    }
    /**
     * Get the current store's base URL.
     *
     * @return string
     */
    public function getStoreBaseUrl()
    {
        return $this->storeManager->getStore()->getBaseUrl();
    }
}


To use the helper class in your module, you need to inject it into the class where you want to use it. For example, if you want to use the helper class in a controller, you can inject it in the controller's constructor.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php

namespace Vendor\Module\Controller\Index;

use Vendor\Module\Helper\Data;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;

class Index extends Action
{
    /**
     * @var Data
     */
    protected $helper;
    /**
     * @param Context $context
     * @param Data $helper
     */
    public function __construct(
        Context $context,
        Data $helper
    ) {
        $this->helper = $helper;
        parent::__construct($context);
    }
    /**
     * Index action
     *
     * @return void
     */
    public function execute()
    {
        $baseUrl = $this->helper->getStoreBaseUrl();
        // ...
    }
}

That's it! You have now created a helper class in Magento 2.

Member

by lizzie , 9 months ago

@shyann 

To create a helper class in Magento 2, you need to follow these steps:


Step 1: Create the helper class file

  • In your module's directory, create a new folder named "Helper".
  • Inside the Helper folder, create a PHP file with the name of your helper class, for example, "Data.php".
  • The file path will be: app/code/[Vendor]/[Module]/Helper/Data.php


Step 2: Define the helper class

  • Open the Data.php file and add the following code to define your helper class:
1
2
3
4
5
6
7
8
9
<?php
namespace [Vendor][Module]Helper;

use MagentoFrameworkAppHelperAbstractHelper;

class Data extends AbstractHelper
{
    // Helper class code here
}


Step 3: Add helper methods

  • Inside the Data class, you can add your helper methods. These methods will contain the logic you want to use in your module.
  • For example, you can add a method to calculate the total price:
1
2
3
4
public function calculateTotalPrice($price, $quantity)
{
    return $price * $quantity;
}


Step 4: Use the helper class in your module

  • To use the helper class in your module, you need to inject it into your controller, block, or any other class where you want to use it.
  • For example, in a controller, you can add the following code to use the helper class:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
use [Vendor][Module]HelperData;

class Index extends MagentoFrameworkAppActionAction
{
    protected $helper;

    public function __construct(
        MagentoFrameworkAppActionContext $context,
        Data $helper
    ) {
        $this->helper = $helper;
        parent::__construct($context);
    }

    public function execute()
    {
        $price = 10;
        $quantity = 5;
        $totalPrice = $this->helper->calculateTotalPrice($price, $quantity);
        // Do something with the total price
    }
}


That's it! You have created a helper class in Magento 2 and can now use its methods in your module.