The Leanpub 60 Day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms...
Kick off your book project in 2 hours! Live workshop on Zoom. You’ll leave with a real book project, progress on your first chapter, and a clear plan to keep going. Tuesday, June 16, 2026. Learn more…
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â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
firstAbc / Hello / Controller / Index / Index.php
firstvar / generation / Abc / Hello / Controller
Try this link to make sure you understand above
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.
Click the buttons to get the free sample in PDF or EPUB, or read the sample online here
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms...
We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.
Learn more about writing on Leanpub
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them
You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!
Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.
Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.