Magento 2

Create a helper in a module in Magento 2

Create: app/code/Company1/Module1/composer.json
 
{
    "name": "company1/module-module1",
    "description": "",
    "require": {
        "php": "~5.5.0|~5.6.0|~7.0.0",
        "magento/framework": "100.0.*",
        "magento/module-ui": "100.0.*",
        "magento/module-config": "100.0.*",
        "magento/module-directory": "100.0.*"
    },
    "type": "magento2-module",
    "version": "100.0.0",
    "license": [
        "OSL-3.0",
        "AFL-3.0"
    ],
    "autoload": {
        "files": [ "registration.php" ],
        "psr-4": {
            "Company1Module1: ""
        }
    }
}
Create: app/code/Company1/Module1/registration.php
 
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Company1_Module1',
    __DIR__
);
 
Create: app/code/Company1/Module1/etc/module.xml
 
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Company1_Module1" setup_version="2.0.0">
        <sequence>
            <module name="Magento_Directory"/>
        </sequence>
    </module>
</config>
 
Module creation done. So now create helper class inside Helper folder.
 
app/code/Company1/Module1/Helper/Data.php:
 
namespace Company1/Module1/Helper;
 
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    public function someMethod()
    {
        return 1;
    }
}
How to use helper class inside controller
 
$this->_objectManager->create('Company1Module1HelperData')->someMethod();
How to use helper class inside block
 
public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Company1\Module1\Helper\Data $helper,
        array $data = []
    ) {
        $this->helper = $helper;
        parent::__construct($context, $data);
    }
So $this->helper is now instance of Data.
 
For overwrite any class you can use preference.
 
app/code/Company1/Module1/etc/di.xml:
 
<xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="\Magento\Directory\Helper\Data" type="\Company1\Module1\Helper\Data" />
</config>

You can also use plugin. Plugin is the best way to overcome rewrite conflict. for more information http://goweb.vn/kien-thuc-web-vi/thiet-ke-web/magento-plug-ins-1204.html

1 Bình luận

  1. advance nói:

    SPECIAL OFFER : cash advance loans in virginia For Women Herbal on SALE , Buy Viagra For Women Herbal shop online .

Trở về
  • Magento plug-ins
  • Magento plug-ins

    Magento2 gave very good concept called Plugin   we can do what ever after and before core function and also we have...

  • HOW TO CREATE MENU IN MAGENTO 2 ADMIN
  • HOW TO CREATE MENU IN MAGENTO 2 ADMIN

    magento-2-admin- menu My previous tutorial show you how to create a simple module in Magento 2, learn more here: http://www.venustheme.com/how-to-create-magento-2-module/....