In magento 2, the creation of module has changed completely. We will go through how to create a module. Like earlier magento, we do not need to put our module files in different folders for code, theme, skin, etc. All, the codes related to the module should be inside one main folder. You can find the sample code in the git repo.
The easiest way to start with code is to put the complete module codes inside app/code/<VenderName>/<ModuleName>
Basic file stucture inside the module.
- Block # block class files
- Controller # controller class files
- etc # xml files
- Helper # Helper class files
- Model # Model class files
- view # theme and skin files
composer.json
LICENSE.txt
README.md
registration.php
The composer.json file looks like below
{ "name": "vendorname/module-samplemodule", "description": "Magento 2 sample module", "require": { "php": "~5.5.0|~5.6.0|~7.0.0", "magento/module-backend": "100.0.*", "magento/framework": "100.0.*" }, "type": "magento2-module", "version": "100.0.0", "license": [ "GPL-3.0" ], "autoload": { "files": ["registration.php"], "psr-4": { "VenderName\\SampleModule\\": "" } } }
The registration.php file looks like below
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'VenderName_SampleModule',
__DIR__
);
Inside etc folder, module.xml file is required
<?xml version="1.0" encoding="UTF-8"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="VenderName_SampleModule" setup_version="2.0.0"> <sequence> <module name="Magento_Core"/> <module name="Magento_Backend"/> </sequence> </module> </config>
Now the basic module is ready. We just need to enable the module. The below code is to enable/disable a module by command line.
# enable a module
php bin/magento module:enable --clear-static-content VendorName_ModuleName
# disable a module
php bin/magento module:disable --clear-static-content VendorName_ModuleName
# upgrade the system, essentially this will make sure any setup scripts have been run and the current module version saved to the setup_module table.
php bin/magento setup:upgrade
# confirm our module has been enabled!
php bin/magento module:status
After running the above command, your module will be displayed in the file app/etc/config.php.You can also check in Stores > Configuration > Advanced > Advanced.
Make sure that the pub and var folder are writable by the command line user for php bin/magento command to work.
Comments
Post a Comment