Learn how to create a custom extensions in Magento 2
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
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.
Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.
You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!
So, there's no reason not to click the Add to Cart button, is there?
See full terms...
Earn $8 on a $10 Purchase, and $16 on a $20 Purchase
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 earnedover $14 millionwriting, publishing and selling on Leanpub.
Learn more about writing on Leanpub
Free Updates. DRM Free.
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