Magento 2 uses module concept as the main tool to extend or alter Magento functionality. Every module contains a chunk of PHP code that is related to the specific task like new widget, new payment method or changing the way taxes are calculated.
Installing and enabling a module
There are two main ways to install a module:
Manually
1. Download or purchase extension.
2. Upload extension to Magento root directory.
Your new module should be located in app/code directory. Example path: app/code/ModuleVendor/ModuleName
3. Enable module.
In order to enable your new module, you should run this command in the terminal as a Magento file system owner:
1 |
php bin/magento module:enable ModuleVendor_ModuleName |
Ecommerce Development
Take your online store to the next level with BelVG Ecommerce Development
Visit the page4. Run setup.
After enabling the new module, you need to upgrade the Magento database. In order to perform this, do this run:
1 |
php bin/magento setup:upgrade |
Via Composer tool
Installing a module via composer is a more reliable way to install modules. Composer automatically installs all required dependencies and makes sure that your module won’t be conflicting with others.
In order to install a module via composer, you need to do the following:
1. Purchase extension.
2. Use Composer tool to install a module.
As a Magento file system owner, run:
1 |
composer require vendor/module-example:dev-master |
3. Follow steps 3 and 4 from manual installation.
Writing simple module
In order to create a basic module, first, you need to prepare your Magento installation for development. This includes enabling developer mode and turning caching off to be able to see all your changes immediately and be aware of any errors that might come up.
To change Magento mode, you need to log in as Magento file system owner and run the command:
1 |
php bin/magento deploy:mode:set MODE_NAME |
So to switch Magento into developer mode run:
1 |
php bin/magento deploy:mode:set developer |
To disable caching you can run:
1 |
php bin/magento cache:disable |
Creating module
All custom modules should be placed into app/code directory. Unlike Magento 1, there are no code pools. Create directory like app/code/ModueVendor/ModuleName
Then create registration.php file in this folder.
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'ModuleVendor_ModuleName', __DIR__ ); |
This file is responsible for registering your module. The second required file is module.xml file which should be placed in etc subfolder of our module.
1 2 3 4 5 |
<?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="ModuleVendor_ModuleName" setup_version="1.0.0"> </module> </config> |
This file provides Magento with information regarding the version of our module and optional dependencies.
Magento 2 Development
Take your online store to the next level with BelVG Magento 2 Development
Visit the page