Magento 2 Build simple module

Full stable magento 2 has release and now everyone shifting with the new Magento world. Magento 2 have change almost all the folder structure so it made little difficult for the developer to move with the folder structure.You can easily build your own module in earlier version of magento.Magento 2 have the same feature.Following is the example for building simple simple module which display Welcome message.

Now lets begin.

Create code folder inside your app folder .In previous version of magento there is local/community folder but magento 2 doesnot need these folders.Now create following directories

  • app/code/Demo/Welcome/
  • app/code/Demo/Welcome/Block/
  • app/code/Demo/Welcome/Controller/
  • app/code/Demo/Welcome/Controller/Index/
  • app/code/Demo/Welcome/etc/
  • app/code/Demo/Welcome/etc/frontend
  • app/code/Demo/Welcome/view/
  • app/code/Demo/Welcome/view/frontend/
  • app/code/Demo/Welcome/view/frontend/layout
  • app/code/Demo/Welcome/view/frontend/templates

and create the following files with a directory

  • app/code/Demo/Welcome/
  • app/code/Demo/Welcome/registration.php
  • app/code/Demo/Welcome/Block/Main.php
  • app/code/Demo/Welcome/Controller/Index/Index.php
  • app/code/Demo/Welcome/etc/module.xml
  • app/code/Demo/Welcome/etc/frontend/routes.xml
  • app/code/Demo/Welcome/view/frontend/layout/welcome_index_index.xml
  • app/code/Demo/Welcome/view/frontend/templates/welcome.phtml

Now in app/code/Demo/Welcome/registration.php

 

And in  app/code/Demo/Welcome/etc/module.xml

in app/code/Demo/Welcome/etc/frontend/routes.xml

in app/code/Demo/Welcome/Controller/Index/Index.php

in app/code/Demo/Welcome/Block/Main.php

and in app/code/Demo/Welcome/view/frontend/layout/welcome_index_index.xml

and in app/code/Demo/Welcome/view/frontend/templates/welcome.phtml

Now You are near to be done.You need to list your module in app/etc/config.php

Add “Demo_Welcome=>1” at the buttom of the code

Screenshot_3

Now you need to run composer or you can do it with git bash.In composer or git bash run

rm -rf var/cache/* var/page_cache/* var/generation/*       (for clearing cache)
php f bin/magento module:enable clearstaticcontent Module_Name
php f bin/magento setup:upgrade

Now go to url yourproject/welcome  ,in my case it is http://localhost:8080/magento2/welcome. You will see

Screenshot_4

That is all .

 

Comments

comments