HOW TO CREATE MODULE IN MAGENTO 2
Creating a module in Magento 2 allows you to extend the functionality of the platform by adding custom features, modifying existing functionality, or integrating third-party services. Below are the steps to create a basic module in Magento 2:
- Set Up the Module Directory Structure:
- Navigate to the app/code directory in your Magento 2 installation.
- Create a new directory for your module using the following format: VendorName/ModuleName.
- For example, if your vendor name is MyCompany and your module name is CustomModule, the directory structure would be app/code/MyCompany/CustomModule.
- Inside your module directory, create a file named registration.php.
- Add the following content to register your module:Create the Module Registration File:
-
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'MyCompany_CustomModule',
__DIR__
);
- Create the Module Configuration File:
- Inside your module directory, create a directory named etc.
- Inside the etc directory, create a file named module.xml.
- Add the following content to define your module:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="MyCompany_CustomModule" setup_version="1.0.0"/>
</config>
- Run the following command from the Magento root directory to enable your module:Enable the Module:
-
php bin/magento module:enable MyCompany_CustomModule
- Inside your module directory, create a directory named Controller.
- Insidethe Controller directory, create another directory named Index.
- Inside the Index directory, create a file named Index.php.
- Add the following content to create a basic controller:Create a Basic Controller (Optional):
-
namespace MyCompany\CustomModule\Controller\Index;use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;class Index extends Action
{
protected $pageFactory;public function __construct(Context $context, PageFactory $pageFactory)
{
$this->pageFactory = $pageFactory;
parent::__construct($context);
}public function execute()
{
return $this->pageFactory->create();
}
}
- Inside your module directory, create a directory named etc.
- Inside the etc directory, create a file named routes.xml.
- Add the following content to define a route for your module:Create a Route Configuration File (Optional):
-
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="custommodule" frontName="custommodule">
<module name="MyCompany_CustomModule"/>
</route>
</router>
</config>
- Flush the Magento cache by running:Verify the Setup:
-
- Check if your module is enabled by running:
php bin/magento cache:flush
php bin/magento module:status
- Check if your module is enabled by running:
- Access Your Module:
- If you created a controller, you can access it by navigating to http://your-magento-url/custommodule/index/index.
By following these steps, you can create a basic module in Magento 2 and start extending the platform’s functionality according to your requirements.