In this blog post we will see how to create simple “hello world” module in magento2.
In magento2 all modules reside in the folder app/code, previously in magento1 there was the concept of local/ community/ core/ folders but that has been removed now.
In this blog post we will see how to create a new module, create a route and display “hello world”
Step1
Module names in magento2 are divided into two part “VendorName_ModuleName”
e.g Magento_Contact, Magento_Catalog or Excellence_Test
first part is the vendor and second part is the actual module.
Let’s take our module name to be “Excellence_Hello”. First we need to make the folders
app/code/Excellence/Hello
Step2 – module.xml
Next we need to add the module.xml file
app/code/Excellence/Hello/etc/module.xml
contents are
<?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="Excellence_Hello" setup_version="0.0.1"/> </config>
Step3 – registration.php
Next need to add a registration.php
app/code/Excellence/Hello/registration.php
content would be
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Excellence_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: Excellence_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 Excellence_Hello
this should enable your module.
Another way of doing this is, go to the file
app/etc/config.php
you will see a long list of modules there, just add your module as well
... 'Excellence_Hello' => 1, ....
this should enable your module as well.
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.
You can also see your module at System Configuration -> Advanced -> Disable Modules Output
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
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
Excellence/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>
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/excellence/*
Next we need to define our controller,action.
Lets assume we want our url to be
www.mymagento.com/excellence/hello/world
for this we need to create the following folder
Excellence/Hello/Controller/Hello/World.php
and add the following content in
World.php
file
<?php namespace Excellence\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; } }
If you have followed all steps properly and open the url in browser you see the output “Hello World”
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/excellence would find path
Excellence/Hello/Controller/Index/Index.php
2. Another important thing, magento create auto generated files at var/generation/Excellence/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.
Try Out this yourself to make sure you understood above
1. Create a url like /excellence_test/hello_world/test
2. Create a url like /excellence/world
3. Create a url like /excellence