Creating Custom Module for Magento 2

Prerequisites

To start with a Magento 2 module development, we want to give emphasis on two things:

1. Disable Magento cache
Firstly, to avoid manually flush the cache every time you make changes to your code, you should disable Magento cache during development.
The process is as usual: Admin → System → Cache Management → select all cache types and disable them.

2. Put Magento into a developer mode
Secondly, developer mode needs to be on to make sure that you catch all the errors Magento throws during the development.
From Magento 2 root. and through terminal, you should run the following command to begin the developer mode on:

Creating the module files and folders:

Download full module from our github

Module setup
In Magento 2 files are not distributed through the folders and have a modular structure. Here the modules are grouped by namespace and placed directly in the app/code folder. You can forget about code pools.
So our first step is to create the module folder and necessary files required to register a Magento module:

Create the following folders

Here, the Scriptlodge folder is the module’s namespace, and Helloworld is the module’s name.
Note: You will have to create it manually if you don’t have the code folder in your app directory.

Creating module.xml
Now in the module folder, we need to create a module.xml file in the app/code/Scriptlodge/Helloworld/etc folder with the following code:

Creating registration.php
With the following code you need create a registration.php file in the app/code/Scriptlodge/Helloworld folder to register the module:

At this stage, you need to clear Magento cache from admin, or through terminal from Magento 2 root with the following command:

Now the extension is supposed to start working. The presence of the module can be checked from Admin → Stores → Configuration → Advanced → Advanced
or you can open app/etc/config.php and check the array for the ‘Scriptlodge_Helloworld’ key, whose value should be set to 1:
module-install1

Creating router:

Basically two routers are needed to create, one for frontend and other for beckend/admin part.
The following routes.xml file is created for frontName and id setting to define queries for the extension.

1. Frontend router

The app/code/Scriptlodge/Helloworld/etc/frontend folder contains the following code for front-end:

Look the frontend router and route is defined with an id “helloworld”.

2. Backend/admin router
Similarly, the app/code/Scriptlodge/Helloworld/etc/adminhtml folder is used for backend:

Here the backend router and route is defined with an id “scriptlodge_helloworld”.

Creating a controller

The first part of the URL is generated from the frontName attribute.

Here you can see how the URLs are constructed in Magento 2:

So, the final URL will emerge in the following way:
helloworld/index/index

Now the Index.php controller file in the app/code/Scriptlodge/Helloworld/Controller/Index folder will have the following code:

Though in Magento 1 each controller can have multiple actions, in Magento 2 every action has its own class to implement the execute() method.

Creating a block:

To show something on the frontend, let’s create a block and a template for it.

Firstly, a Helloworld.php file in the app/code/Scriptlodge/Helloworld/Block folder with the following code will inherit from Magento class \Magento\Framework\View\Element\Template:

Creating a layout and template files:

Templates and layout files reside in view folder inside the module. So, we find three sub-folders under view:
a. adminhtml,
b. frontend,
c. base
And the above folders serve the following purposes respectively – adminhtml folder is used for admin, the frontend folder is used for frontend and the base folder is used for both, admin & frontend files.

Layout
First we will create a helloworld_index_index.xml file in the app/code/Scriptlodge/Helloworld/view/frontend/layout folder with the following code:

A block has been added to the content container in the layout and set the template of our block to helloworld.phtml, and the styles file to use when showing on frontend. The template and styles file has the following address:

Template
A helloworld.phtml file in the app/code/Scriptlodge/Helloworld/view/frontend/templates folder will have the following code:

Styles, Js and Images
And a hello.css file in the Scriptlodge\HelloWorld\view\frontend\web\css\hello.css.
In the same way, the images and js folders will reside into web folder.

Create admin settings:

To create some settings for the new extension it will be needed to add adminhtml folder to Scriptlodge\HelloWorld\etc folder. It will contain configuration files for backend.

Here, a routes.xml is needed for frontName and id setting, which is used to define queries for the extension. It was created in our previous section (Creating router).

The xml markup is slightly different in Magento 2 but in the previous version, they’re located in the system.xml file.

Here comes the two most popular settings as an example: a text setting and a yes/no setting.
Looking at the config file contents you can know what it actually means:

Add a new section to the settings block using . Set the unique ID and the name of the settings section inside. It is done because we have a very high possibility of using several apps from the same vendor on a single site.

Add the new

again, with the unique ID, and set the necessary parameters, such as type, translation fields, order, label, the block for which the section is added, and so on. And then add the settings just inside the section. They will be divided into groups (one group in our case). The groups are defined by , and the fields are set by . We have already created three settings and pointed out types, labels, visibility, translation, comments and the data model.

Let’s check the result. Open your Magento 2 backend, go to Stores – Configuration, we can see the settings!

module-config

Creating Helper:

Now we create the helper in the following path:

And we add \Magento\Framework\App\Config\ScopeConfigInterface interface object initialization to the helper, it works to receive the data from configuration.
Now the file looks like this:

Let’s use these functions in the block and the template, so we need to change block function and template:

Block file:

Template file:

Creating Model files:

Create installation script/Schema setup scripts
As in the 1.x versions of Magento, we need to create the installation file to use our own table.

In magento 2 schema setup scripts change database schema, they create or change needed database tables. If module is installing, Setup\InstallSchema::install() is executed.
The file should be created here: Scriptlodge\HelloWorld\Setup\InstallSchema.php

We can see that we are creating a “scriptlodge_helloworld” table with one field of integer type and four fields of text type.
Current module version is saved in setup_module table, which is equivalent of core_resource in Magento 1. Magento regulates what needs setup by comparing module versions with what it sees in that table.

Create Model

As in the Magento 1.x version, we need to create three classes to work with the model: the model itself, the resource model and the collection.

Let’s create a Scriptlodge\HelloWorld\Model\HelloWorld.php file and use the following initialization:

Create a Scriptlodge\HelloWorld\Model\ResourceModel\HelloWorld.php file and use the following initialization:

Create a Scriptlodge\HelloWorld\Model\ResourceModel\HelloWorld\Collection.php file and use the following initialization:

We just set the constructor for the model here. In fact, there is no great difference from the Magento1.x versions on this step.

Now we are going to check if the created model actually works. Create a simple function in the block we’re working with:

And also update Block __construct function,

Finally use these functions in the block and change the template file:

Finally deploy the module

module-data-view

One thought on “Creating Custom Module for Magento 2”

Leave a Reply to Abu Wahid Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">