Email the Author
You can use this page to email Brian about Learn how to create a custom extensions in Magento 2.
About the Book
In this blog post, we will see how to create simple "hello world" module in magento2.
In magento2 all the modules are in the application / code directory, earlier in magento1 had the concept of locale / community / core / directory but has been removed now. In this blog post, we will see how to create a new module, create a route and display "hello world".
Step 1: Create a directory
Module name in magento 2 is divided into two parts "VendorName_ModuleName"
for example Magento_Contact, Magento_Catalog or Abc_Test the first part is the vendor and the second part is the actual module.
Let's take our module name "Abc_Hello". First we need to make the directories
Step 2 - module.xml
Next we need to add files
module.xml
app / code / Abc / Hello / etc / module.xml
Bước 3 - registration.php
We will add registration.php in
app/code/Abc/Hello/registration.php
with the following code
<?php\Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Abc_Hello', __DIR__);
Bước 4: chạy lệnh kích hoạt module
Bây giờ một module rỗng là sẵn sàng, bây giờ chúng ta cần phải kích hoạt nó.
Ở giai đoạn này nếu bạn chạy lệnh
php bin/magento module:statusList of disabled modules: Abc_Hello
This means the module is set up, but it is turned off now.
To activate the modules, run the command
php bin/magento module:enable Abc_Hello
This will enable your modules. Another way to do this is, go to file
app/etc/config.php
You will see a long list of those modules, just add your module as well
... 'Abc_Hello' => 1, ...
This will enable your modules as well.
After this step, when you open your website in your browser you will get an error saying
Please upgrade your database: Run "bin/magento setup:upgrade" from the Magento root directory.
So run the command:
bin/magento setup:upgrade
Step 5 - Path
Now lets add a route (or url) to our module so we can show "hello world"
The route in magento is divided into 3 parts
http://localhost.com/index.php/route_id/controller/action
index.php is optional and depends on your Magento configuration. If you have .htaccess index.php the file is not required.
To add routes we need to add routes.xml file
Abc/Hello/etc/frontend/routes.xml
Since this is a path, we've added it to the frontend / directory we need to add it to adminhtml / directory
The contents 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="abc" frontName="abc"> <module name="Abc_Hello" /> </route> </router></config>
We usually keep the id and frontName the same, or it could cause some problems. Here we define the beginning of our route. So until now our path is
http://localhost.com/abc/*
Next we need to define our controller action. Let's assume we want our url to be
www.mymagento.com/abc/hello/world
for this we need to create the following directory
firstAbc / Hello / Controller / Hello / World.php
and add the following in
<?phpnamespace Abc\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 the steps properly and open the URL in your browser see the output “Hello World”
Note
- There is also one important thing to note, if you miss a control or action the name automatically defaults to Index. Meaning, a url like www.localhost.com/abc will find the excellent path
firstAbc / Hello / Controller / Index / Index.php
- Another important thing, magento generated automatically generated files in
firstvar / generation / Abc / Hello / Controller
- . So if you Realize that, do you make changes to controls and the changes are not displayed. Be sure to delete the generated cache files.
Try this link to make sure you understand above
- Create a url like / abc_test / hello_world / test
- Create a url like / abc / world
- Create a url like / abc
I have also published an article that review top 5 best magento 2 hosting providers. If you are struggling in finding a good magento2 hosting provider
About the Author
My name is Hung Tran, I’m the owner and content creator of magentip.com, where I share my knowledge about everything related to Magento and Magento 2.