How to create custom module and create custom page in Magento2

Blog
Spread the love

Magento which is now Adobe Commerce, is an open source e-commerce plateform written in PHP. Magento provide all required functionalities for e-commerce business. Apart from that Magento site owners and users require some of the custom feature to meet their business needs or sometimes it needs to override the default Magento functions as per the business logic and requirements.In these situations Magento developers needs to create module and implement logic based on the business requirements.

In ordre to create custom module, you need to first install Magento 2 either locally or on a development server, that you can add new files to.

If you have not yet installed Magento2, you can follow the steps How to install Magento2 on VmWare Or Ubuntu

We are going to implement this in following steps:

  • Step 1: Create Magento 2 Module Structure.
  • Step 2: Declaration of Module.
  • Step 3: Registration of Module.
  • Step 4: Activate the Module.
  • Step 5: Add functionality – Create custom page with bnews/index/view.

Let’s ger started:

Step 1 Create Magento2 Module Structure

You can add module in app/code directory or in vendor/magento directory.

Example:

Magento to configuration

In the above image is showing standard module folder structure which is Magento CMS module.

1. Create the following folders in the magento project directory

app/code/Bluethink/CustomModule

Bluethink is the module’s Vendor name and CustomModule is the Module name.It could be diffrent Name and app/code should be the same.

Note: If you don’t have the code folder, create it manually.

Step 2: Declaration of Module

Declaration of Module is exist in it

app/code/Bluethink/CustomModule/etc/module.xml

It is necessary to create etc folder and add the module.xml file.

Contents would be:

Explanation:

xmlns:xsi:http://www.w3.org/2001/XMLSchema-instance —Declared in the XML and is used to help declare that the XML document is an instance of and XSD. The convention is to use the namespace prefix of xsi. targetNamespace: is used in the header of the XSD to defined the namespace that the XSD describes.

Module.xsd file describe Naming convention of module attribute, sequence, and setup version (schema version).

Visit:vendor/magento/framework/Module/etc/module.xsd to all about attribute of module.xml file.

Module attribute : define the name of module.

Sequence attribute : Module written in sequence attribute triggers serial wise before parent module.

Step 3: Registration of Module

To register the module, create a registration.php file in the

app/code/Bluethink/CustomModule/registration.php

Contents would be:

Explanation:

There is called register method with three arguments from ComponentRegistrar class.
Visit: vendor/magento/framework/Component/ComponentRegistrar.php

Magento to configuration

Type : ComponentRegistrar class register five type of component in Magento by using register method. Name of the component given below Module, Theme, Language, Setup, and Library.

ComponentName: it will be the name of the component. In the above example given the name of the module – Bluethink_CustomModule.

Path: Third argument __DIR__ is a magic method of PHP. It will be return the path of the file.

Step 4 : Activate the module

Now its time to enable our moule. Lets check the status of our module by typing below command in the magento project root directory.

php bin/magento module:status

You can see Bluethink_CustomModule is disable now.

So lets run the command to enable our module:

php bin/magento module:enable Bluethink_CustomModule

Now that you have enabled the module first time, so you have to upgrade the database of the module by using this command line:

php bin/magento setup:upgrade

Step 5 Add functionalities – Create custom page with “bnews/index/view”

In Magento url is represented by frontname/controllerPath/action.So in this case
bnews is frontname, index is controllerPath, and view is action.

Final URL: http:/ bnews/index/view

The front name as bnews in the router file will be used to access the module through the URL.

Step 5.1 Create routes.xml in it

app/code/Bluethink/CustomModule/etc/frontend/routes.xml

folder “frontend” is used to create route for storefront and for the admin route we need to create routes.xml under adminhtml. We will learn about the admin routes later.

Contents would be:

Explanation:

routes.xsd file define about all attribute like naming convention of routerID and routerIdType etc.

Visit:vendor/magento/framework/App/etc/routes.xsd

Magento to configuration

router id: define area of Magento.

Standard: define for storefront (frontend).

admin: define for Magento backend(admin pannel).

route frontName: It is used to access controller action.

route id: It is used to create layout file name.

module attribute : The route will be used for the module given in module attribute.

Step 5.2Create Controller and Action

controller is the folder inside Controller folder. One of the important in Magento system is frontController(Magento\Framework\App\FrontController), it always receives request then route controller, action by route_name Let’s take an example of routing an request:

Magento to configuration

If there is an action of controller class found, execute() method will be run.

Let’s create View Controller class in it

app/code/Bluethink/CustomModule/Controller/Frontend/Index/View.php

Similarly, Frontend is used to storefront and Adminhtml is used for Magento Backend.

Content would be:

Here View class extends abstract class \Magento\Framework\App\Action\Action. Action class helps to take and perform action by controller.

Now, if you mention url http:/bnews in the browser, Magento will call automatically http:/bnews/index/index, so we need to mention http:/bnews/index/view.

When control comes in controller view class, execute method will automatically executed by Magento\Framework\App\FrontController::dispatch() on line $actionInstance->execute();
In this method returning Magento\Framework\View\Result\Page Object, This will trigger layout rendering process.

In Magento2 all controller actions must be return something. Return type is define that which action should be perfome.

Example of Result and instance of:

\Magento\Framework\Controller\Result\Json– returns a json object.

\Magento\Framework\Controller\Result\Raw– returns raw string or text.

\Magento\Framework\View\Result\Page– renders html content.

\Magento\Framework\Controller\Result\Redirect– redirects action to another action.

\Magento\Framework\Controller\Result\Forward– forwards to an other action (internal redirect).

Step 5.3 Create layout Structure for the frontend

Magento gives authority to customize page structure using layoute file which is define under view/{area-path}/layout folder.

The area path can be frontend or adminhtml.The Layout file is a xml file and a major path of view layer in Magento 2 Module.

There is a layout file name default.xml file. This file will be applied all the pages in it’s area.Otherwhile the layout file have following format as:

{router-id}_{controller-name}_{action-name}.xml

Magento will check the layout file to find the handle for the page and then load Block and Template. We will create a layout handle file for this module in it:

app/code/Bluethink/CustomModule/view/frontend/layout/custommodule_index_view.xml

content would be:

Here we can see layout=”1column” in page tag. The layout attribute is decide grid structure of the page. Magento provides below types of layout for frontend:

empty

1column

2columns-left

2columns-right

3columns

and 3 page layout types for the backend (admin-empty, admin-1column, admin-2columns-left).

Above layout is defined in vendor/magento/module-theme/view/frontend/page_layout

An Example of 3columns layout:

Magento to configuration

In the layout file, we will block and template.

name: it is required attribute and is used to identify block and template

Step 5.4 Create Template page

And to finish this route’s layout, here is our PHTML template, app/code/Bluethink/CustomModule/view/frontend/templates/custompage.phtml:

content is here:-

Save the file and please run php bin/magento cache:clean to check result..

Let’s hit the url :

Magento to configuration

bluethinkinc_blog

bluethinkinc_blog

2022-11-22

0

Leave a Reply

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

Find More Blogs

How to Add a Custom Tab to the Customer Order Detail Page in Magento 2

Spread the love Adding a custom tab to the Order Detail

Exception Handling in Java

Spread the love Introduction – Exception:An unwanted

How to add custom less file in Magento2

Spread the love Source files *.css included into layout

Spring Boot Microservices

Spread the love Microservices architecture has become

Implementation of WireMock for better testing

Spread the love WireMock is a flexible and powerful tool

Magento 2: How to selected default option in configurable options?

Spread the love Configurable products in Magento 2 allow

How To Implement Simple JWT Authentication in Django Rest Framework

Spread the love We’ll learn how to use simple JWT

Optimizing Search: Integrating Elasticsearch in Django Applications

Spread the love What is Elasticsearch? Elasticsearch is a

How to create Customer Segments

Spread the love In Magento 2, “customer

Proxy Design Pattern and Requisition List in Magento 2

Spread the love Requisition List in Magento 2 Requisition

bluethinkinc Blogs