Email the Author

You can use this page to email Brian about Learn how to create a custom extensions in Magento 2.

Please include an email address so the author can respond to your query

This message will be sent to Brian

This site is protected by reCAPTCHA and the Google  Privacy Policy and  Terms of Service apply.

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

  1. 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
  1. Another important thing, magento generated automatically generated files in
firstvar / generation / Abc / Hello / Controller
  1. . 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

  1. Create a url like / abc_test / hello_world / test
  2. Create a url like / abc / world
  3. 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

Brian’s avatar Brian

@magentip

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.

Logo white 96 67 2x

Publish Early, Publish Often

  • Path
  • There are many paths, but the one you're on right now on Leanpub is:
  • Magentip › Email Author › New
    • READERS
    • Newsletters
    • Weekly Sale
    • Monthly Sale
    • Store
    • Home
    • Redeem a Token
    • Search
    • Support
    • Leanpub FAQ
    • Leanpub Author FAQ
    • Search our Help Center
    • How to Contact Us
    • FRONTMATTER PODCAST
    • Featured Episode
    • Episode List
    • MEMBERSHIPS
    • Reader Memberships
    • Department Reader Memberships
    • Author Memberships
    • Your Membership
    • COMPANY
    • About
    • About Leanpub
    • Blog
    • Contact
    • Press
    • Essays
    • AI Services
    • Imagine a world...
    • Manifesto
    • More
    • Partner Program
    • Causes
    • Accessibility
    • AUTHORS
    • Write and Publish on Leanpub
    • Create a Book
    • Create a Bundle
    • Create a Course
    • Create a Track
    • Testimonials
    • Why Leanpub
    • Services
    • TranslateAI
    • TranslateWord
    • TranslateEPUB
    • PublishWord
    • Publish on Amazon
    • CourseAI
    • GlobalAuthor
    • Marketing Packages
    • IndexAI
    • Author Newsletter
    • The Leanpub Author Update
    • Author Support
    • Author Help Center
    • Leanpub Authors Forum
    • The Leanpub Manual
    • Supported Languages
    • The LFM Manual
    • Markua Manual
    • API Docs
    • Organizations
    • Learn More
    • Sign Up
    • LEGAL
    • Terms of Service
    • Copyright Policy
    • Privacy Policy
    • Refund Policy

*   *   *

Leanpub is copyright © 2010-2025 Ruboss Technology Corp.
All rights reserved.

This site is protected by reCAPTCHA
and the Google  Privacy Policy and  Terms of Service apply.

Leanpub requires cookies in order to provide you the best experience. Dismiss