Creating a new module in magento 2
In magento 2 all modules reside in the folder app/code directory, previously in magento1 there was the concept of local/ community/ core/ folders but that has been removed now.
For creating modules in magento 2 we need at least these 3 files in our module in this directory arrangement.
<?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="Trieffects_Hello" setup_version="0.0.1"/>
</config>
For creating modules in magento 2 we need at least these 3 files in our module in this directory arrangement.
Step1
Module names in magento 2 are divided into two part “VendorName_ModuleName”
e.g Magento_Contact, Magento_Catalog or Trieffects_Test
first part is the vendor and second part is the actual module.
e.g Magento_Contact, Magento_Catalog or Trieffects_Test
first part is the vendor and second part is the actual module.
Let’s take our module name to be “Trieffects_Hello”. First we need to make the folders
app/code/Trieffects/Hello
Step2 – module.xml
Next we need to add the module.xml file
app/code/Trieffects/Hello/etc/module.xml
with following contents
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Trieffects_Hello" setup_version="0.0.1"/>
</config>
Step3 – registration.php
Next need to add a registration.php
app/code/Trieffects/Hello/registration.php
content would be
1
2
3
4
5
6
| <?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Trieffects_Hello' , __DIR__ ); |
Step4
Now an empty module is ready, we now need to enable it.
At this stage if you run the command
php bin/magento module:status
You should see
List of disabled modules: Trieffects_Hello
This means the module is setup, but it is disabled right now.
To enable the module, run the command
php bin/magento module:enable Trieffects_Hello
this should enable your module.
After this step, when you open your website in browser you will get an error saying
Please upgrade your database: Run "bin/magento setup:upgrade" from the Magento root directory.
run the command
bin/magento setup:upgrade
to fix this. At this point you should be all set with a empty module.
Step5 – Routes
Now lets add a route (or url) for our module so that we can display “hello world”
Route’s in magento are divided into 3 parts
Route’s in magento are divided into 3 parts
http://mymagento.com/index.php/route_id/controller/action
index.php is optional and depends on your magento configuration. If you have .htaccess file work index.php is not required.
To add route we need to add routes.xml file
Trieffects/Hello/etc/frontend/routes.xml
since this is a frontend route, we added it in frontend/ folder else we need to add it to adminhtml/ folder
the content of the file are
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
<router id="standard">
<route id="excellence" frontName="excellence"> <module name="Excellence_Hello" />
</route>
</router>
</config>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
<router id="standard">
<route id="excellence" frontName="excellence"> <module name="Excellence_Hello" />
</route>
</router>
</config>
We usually keep id and frontName the same, or it might causes some issues.
Here we defined the first part of our route. So till now our route is
www.mymagento.com/trieffects/*
Next we need to define our controller,action.
Lets assume we want our url to be
www.mymagento.com/trieffects/hello/world
for this we need to create the following folder
Trieffects/Hello/Controller/Hello/World.php
and add the following content in
World.php
<?php
namespace
Trieffects\Hello\Controller\Hello;
class
World
extends
\Magento\Framework\App\Action\Action
{
public
function
__construct(
\Magento\Framework\App\Action\Context
$context
)
{
return
parent::__construct(
$context
);
}
public
function
execute()
{
echo
'Hello World'
;
exit
;
}
}
Important
1. There is another important thing to note, if you miss out controller or action name it automatically defaults to Index. Meaning, a url like www.mymagento.com/trieffects would find path
Trieffects/Hello/Controller/Index/Index.php
2. Another important thing, magento create auto generated files at var/generation/Trieffects/Hello/Controller . So if your noticing that, your making changes to controller and the changes are not showing up. Make sure to delete the cache file generated.
Comments
Post a Comment