We will create a custom module in Magento 2 named “HelloWorld”, that will show output as “Hello World” to the content block using a custom frontend route. So we will create a module with Namespace “Oscprofessionals” and Module Name “HelloWorld”.
“Oscprofessionals_HelloWorld” Before creating the first module, we have to understand the differences in directory structure between the Magento 2 version. In Magento 2, there are no longer any code pools (local, community, and core) in /app/code directory. Code pools are eliminated from the code-base file structure. Magento 2 only has /app/code/. And the modules are grouped by `Vendor`. So we will create module right inside the app/code directory.
Let’s setup our basic custom module structure :
We will need three files at root of the module namely:
"A simple Magento 2 Hello World module.",
Here we see many elements. Lets explain each one of them.
“name” – The name element holds the Composer name by which we will refer to the module.
“description” – Optional description of module.
“type” – This defines our repo as a magento2-module, this is so when someone includes your module as a dependency on their Magento 2 install, composer will know what to do with it. And that brings us to the next bit.
“version” – The version element declares the version of this package.
“license” – License of your module.
“require” – The required element declares which other modules should be loaded and the version number we are compatible with.
“extra” – The extra/map element tells that all the code should go under the Oscprofessionals/HelloWorld directory, which is relative to the app/code directory when type is set to magento2-module.
Step 2: Next, we need a registration.php file in the root of our module. This is picked up by the Magento framework and will handle registering your module with Magento.
Here we define our module name in module tag. Here we can define dependencies of our module. In our example we have defined dependency on the module Magento_catalog. It means our module must be loaded after Magento_Catalog module. In case this module is not available, our module will not be loaded.
Now with these files, we have registered our module.
Step 4: Next, we will create a custom routes for our module. For this purpose, we need to create a routes.xml at etc/frontend folder.
In this file we have defined our router that is of type “standard” and front name is “oscp”.
To register a router we need to add to this: ,
and if we wanted an admin router we do this: .
Next we define our route.
The id attribute is simply an identifier, it should be unique.
The frontName attribute is going to be the first part of the URL, in the complete example our URL is /oscp/helloworld/index
Step 5: With our route registered, now we can create our Controller action. This is something else that has changed in Magento 2 in comparison to 1.x in a fairly major way. Now, instead of specifying a controller class, which has multiple [actionName]Action() methods, you now have a class for each action, which implements a execute() method. Here is the code:
In our example, we have created an additional action class that will list all new products that is coming from a widget created in magento as default. For this purpose, we will create a new action as follows: