Magento 2 sample module

There are lots of blogs and tutorials available for Magento 2 modules. Magento folks already started work on the projects and module development for the version 2. Magento community is doing a great job by sharing their knowledge and ideas.

I just got chance to write my first Magento 2 sample module.  Before starting on Magento 2, developers will need to understand PHP Namespaces concept. I found it pretty simple and having new experience to use it. There are already many other frameworks which are using PHP namespace concept, but for Magento 1 developers it will be a new feature.

First time when I was checking Magento 2 files structure I was little bit confused. People mention in their blogs to write module in app/code folder but I didn’t find code folder in app. All the core modules are exist in vendor/magento folder.

Anyways, I followed different tutorials and got it working.  We need to crate code folder inside app folder. I am going to create module My_SampleModule here.

Let’s start with step by step…

Step 1 : First we need to create our main module configuration file module.xml.

Create it on path app/code/My/SampleModule/etc/module.xml and add below code in it.

Every time creating a new module we just need to change in below line. Everything else will be same as it is.

Where name parameter will be our module name and setup_version will define version of our module.

Step 2 : Now add new module to global module list in config.php file.

Now open a file app/etc/config.php. In this file we can see list of all the module installed in setup. We need to add our module to this list.

Step 3 : This step is also relate to module declaration part.

Create a registration.php  file on  path app/code/My/SampleModule/registration.php and add below code in it.

This is equivalent to app/etc/modules/Namespace_Module.xml file from Magento 1.

Step 4 : After finishing above steps now if you run the site on browser, you will get the error

We need to run below command from terminal to finish the installation of module.

Step 5 : Now let’s create a router (front-name) for our module.

Create a file routes.xml on path app/code/My/SampleModule/etc/frontend/routes.xml and add below content of code in this file.

Here we need to take care about below code and other will be same it is.

We can set id and frontName values same or different. FrontName will be our route name and in our case our URL will be going to look like

http://example.com/my/sample/module

Step 6 : we need to create a controller file for our module.

Create a file on path app/code/My/SampleModule/Controller/Sample/Module.php

In Magento 2, every controller has execute method.  Now to test our controller is working or not, change execute method to look like

And run URL http://example.com/my/sample/module . Check if it print the message or not. If it show any error try to delete cache from admin and clear content from var/generation. You can see the folder called “My” in var/generation after run the URL. We can delete it manually.

We need to import the pageFactory class in our file. We will use this class object to create a page.

Step 7 : Now let’s create our layout file

Create a file app/code/My/SampleModule/view/frontend/layout/my_sample_module.xml.

Here it is very important to take care about the file name. In our case we are going to run the URL my/sample/module so I have created file with name “my_sample_module.xml“.  Add the below content of code in this file.

In the code above we have,

Here we take reference block name as content, We have declare a block class My\SampleModule\Block\Sample and template file sample.phtml.

Step 8 : Creating a block file

Create a block class file on path app/code/My/SampleModule/Block/Sample.php with below code.

This is pretty simple block and template file relation as Magento 1. In block file we can create a different methods for setting and getting data and we can use those methods in our phtml files.

Step 9 : Finally we need to create a template file.

Create a file on path app/code/My/SampleModule/view/frontend/templates/sample.phtml and below code in it.

Now clear your cache and delete “My” folder from var/generation and run URL http://example.com/my/sample/module in browser.