How to Create Magento2 Hello World Example ? or How to Create Custom Extension in Magento2 ?

Share:


Example 1 : How to Create Magento2 Hello World Example 
or
Example 1 : How to Create Custom Extension in Magento2 ?



Hello Friends My name is Vishal Prajapati.

Today We Learn "How to Create Simple Example of Displaying Hello World in Magento2".

Please Follow My Steps for this.

1. Please install Magento2 in your Localhost and. Create folder as mention below.
   We have to create "code/MSG/HelloWorld" folder inside app folder in root directory.

"app/code/MSG/HelloWorld"

2. We have to create "module.xml" file in etc folder.In Magento2 "module.xml" is required 
   File to Create Custom Extension.

"app/code/MSG/HelloWorld/etc/module.xml"

Please Copy Below Content 

<?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="MSG_HelloWorld" setup_version="1.0.0">
    </module>
</config>

3. We have to create "registration.php" file in HelloWorld folder.In Magento2 "registration.php" is required File to Create Custom Extension.

"app/code/MSG/HelloWorld/registration.php"

Please Copy Below Content
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
 \Magento\Framework\Component\ComponentRegistrar::MODULE,
 'MSG_HelloWorld',
 __DIR__
);

In Magento2 to Register Extenstion in We are using  "ComponentRegistrar" class.
We are using "register" method of it.

1. First argument of register method is for "MODULE" register or register Extension
"MSG_HelloWorld" to the Magento.

2. Second argument is Extension name "MSG_HelloWorld"

           We can set Extension name by :

1. MSG is Folder name in app.

                2. HelloWorld is Folder name in app.

 3. Run Setup Upgrade Command in Console.

                 php bin/magento setup:upgrade

     How to check Magento2 Extension is Installed or Active.

          Two ways to check

          1. We can see "MSG_HelloWorld" Extension in this location "app/etc/config.php" 

         2. We can run Command in Console to Check Extension is "Enable/Disable"

                     php bin/magento module:status

    How to  "Enable/Disable"  Custom Extenstion in Magento2 ?

1. We can run Command in Console for "Enable"  Custom Extenstion in Magento2

  php bin/magento module:enable MSG_HelloWorld

  2. We can run Command in Console for "Disable"  Custom Extenstion in Magento2

            php bin/magento module:disable MSG_HelloWorld

4. Create "route.xml" file 

"app/code/MSG/HelloWorld/etc/frontend/routes.xml"

Please Copy Below Content 

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route frontName="helloworld" id="helloworld">
            <module name="MSG_HelloWorld"/>
        </route>
    </router>
</config>

5. Create Controller

"app/code/MSG/HelloWorld/Controller/Index/Test.php"


    Please Copy Below Content 

    <?php
namespace MSG\HelloWorld\Controller\Index;

class Test extends \Magento\Framework\App\Action\Action
{
protected $_pageFactory;

public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $pageFactory)
{
$this->_pageFactory = $pageFactory;
return parent::__construct($context);
}

public function execute()
{
echo "Hello World";
exit;
}
}

We can run Command in Console for clear cache.

 php bin/magento cache:clean 

Now Please run below URL in Browser

http://localhost/projectname/helloworld/index/test

We can view video on Youtube also



Please check Folder Structure so that we can easily understand.




Download  Magento2 Hello World Example Zip File. 
https://github.com/20al/Magento-Study-Guide-MSG-Example-Series.git


1 comment:

  1. Thanks for taking the time to share this, i feel strongly about it and i like to learn more on this topic.It's extremely helpful for me.Visit us on magento app development | magento development company

    ReplyDelete