Clever works_

Our first Module and its controller in Magento 2

MAGENTO 2 DEVELOPER MODE

To put Magento into developer mode, we need to run the following command:

Why do we need it? It will show all the errors for us, disable the static view of file caching, enable automatic code compilation and allow for enhanced debugging. System logging is in var/reports . When we are finished, we can set Magento to production mode by:

It’s faster than developer  mode. Exceptions are not displayed to the users, and static view files are served from the cache only. It gets the files from pub/static  directory.

MAGENTO 2 DISABLE CACHE

To save time, we can disable the cache by going to:

We need to select all and set to disabled. If we don’t do that we will clear the cache after every modification. If we need to clear cache manually we can use:

 

MAGENTO 2 MODULE CONFIGURATION

Firstly, we need to create module.xml in app/code/CleverCode/HelloWorld with the contents:

In the module name we can see the namespace and module name which agree with the folder structure. Our module will be called HelloWorld  and the version will be   1.0.0

In the next step we need to create the file registration.php  in app/code/CleverCode/HelloWorld  with the following contents:

It specifies the path to the directory where the module is placed. We need to do that for all modules every time. Our module is ready, but we need to enable it. We can check the status by this command:

If we see: List of disabled modules: CleverCode_HelloWorld  it means that everything is correct. To enable our module just type:

We can also enable it by editing: app/etc/config.php  and adding 'CleverCode_HelloWorld' =>; 1,  but the first option would be better.

After that to update the system we need to type:

 

MAGENTO 2 ROUTERS

To add routers for our module, we need to create routes.xml in app/code/CleverCode/HelloWorld/etc/frontend  with the following contents:

Why do we need a router? The attribute called frontName  will be used in the URL. We will call this frontName every time we use actions from our module.

 

MAGENTO 2 CONTROLLER

To display any data by using our front name we need to create a controller. Firstly, we will create the file Index.php  in app/code/CleverCode/HelloWorld/Controller/World with contents:

It’s placed in the Controller folder. It contains the execute() method which is the only method. We can’t create more modules called “Action” in Magento 1. To run our controller just type:

or

What does it mean? We are matching the controller action with the router. If we type the correct URL it will call execute()  method. All frontend controllers extend from \Magento\Framework\App\Action\Action . It has a dispatch method which checks if the request is dispatched, a redirect method which redirects to specified URL, and some other methods.
If something doesn’t work, we should try to type:

 

 

Related works_

BreakAwayDistributing B2B and B2C
selling platform

The SLK Solutions agency responsible for developing an online store hired us to help them with creating a selling platform dedicated to B2B and B2C clients. BreakAwayDistributing – agency operating from 1993 with main office located in Hamilton, Canada offers high quality products

Learn more

Internet flower shop
from SLK Solutions

SLK Solutions is an IT agency operating in the USA, which helps small and large business owners to implement new technologies. They asked us to support them with our experts with the complete implementation of an eCommerce project for their client.

Learn more