Get Free Audit

Modules in Magento 2

Dec 21, 2020

3511 Andrey Litvin

Modules in Magento 2

In this article, we will consider the ways to enhance your product with the help of new Magento 2 modules (extensions), specifically, by adding the files to the directory via FTP or with the help of the configuration setting for Composer, as well as their custom structure, admin part, and debugging process.

How to install modules in Magento 2
Dependency injection
Custom structure
The admin part
Debugging process

Magento 2 extensions are used for customization and extending existing Magento functionality or adding new features.

There is a good article in Adobe devdocs with detailed information on the topic.

How to install modules in Magento 2

There are two options how you can do it since the release of the 2.4.0 version:

  • The first one is to add the Magento 2 extension files to app/code/<Vendor>/<Module> directory.
  • The second one is to install it via composer.

There was a third one that was deprecated in Magento 2.3.6 and removed in Magento 2.4.0. It was a Setup Wizard feature.

To begin the extension development in Magento 2, use Modules Manager. You can find more detailed information here.

You can find information on main composer’s commands, and composer config file.

 Option #1 How to install modules in Magento 2

  1. Copy module’s files to app/code/<Vendor>/<Module> directory via FTP.

Modules in Magento 2

Extend functionality with Magento 2 modules

Contact us

Option#2 How to install modules in Magento 2

  1. There is a configuration setting for Composer which indicates the location of the extension’s files. They can be located remotely or locally.

To add the configuration for composer, the following command is used:

composer config repositories.<module_name> path /path/to/magento/module (there could be any path on your server which is convenient for you)

If a repository is located remotely the command could be:

composer config repositories.<module_name> git https://remote-repository-url.com

  1. To run composer <vendor>/<name>:<version> is required

If you do it for the first time Magento will ask you to enter Access Keys. You could get them in your account on https://magento.com/.

You can find an instruction on how to do it here.

After you have placed the required extension files in Magento via Option 1 or Option 2, run the following commands to finish the installation:

If the extension is being installed on the site in a production mode:


If the extension is being installed on a site in developer mode:


Below you will find several articles with detailed information about the commands used for installing extensions and the description of different options that could be selected for more specific purposes.

You can find the instruction on how to enable or disable the maintenance mode here.

In this article, you can read more about code compilation.

The topic of static files deployment is described here.

You can study how to enable or disable modules in more detail here.

More information about production/developer/default Magento modes is provided here.

Dependency Injection

Dependency Injection is an important concept of declaring dependencies of classes. You will find information on DI Concept here, in this article and about DI configuration in Magento 2 here.

Example:

Path is vendor/magento/module-store/etc/di.xml


Path is vendor/magento/module-store/Model/ScopeTreeProvider.php


Class Magento\Store\Model\StoreRepository is a realization of Magento\Store\Api\StoreRepositoryInterface declared in di.xml

So you could include an interface in your class and use the desired realization of it.

Сustom structure

There is an example of a custom structure. The extension implements an ability to set up a date of visiting the showroom. The customer should enter a name, an email, desired date and showroom from a list.

modules-in-magento2-1

To create the form the next structure is required (front end part):

  1. Declare a module

app/code/BelVG/Showroom/composer.json


app/code/BelVG/Showroom/registration.php


app/code/BelVG/Showroom/etc/module.xml

  1. Create controllers

Here is a controller for displaying a form on the site. It should be extended from Magento\Framework\App\Action\Action class.

app/code/BelVG/Showroom/Controller/Booking/Index.php


Here is a controller for saving information from postData and notifying an administrator about booking:

app/code/BelVG/Showroom/Controller/Booking/Save.php


Configuration for controllers is placed in app/code/BelVG/Showroom/etc/frontend/routes.xml

The form will be accessed on the page http://<domain>/showroom/booking/

  1. Create the form displaying on the site

app/code/BelVG/Showroom/Block/Booking.php


The name of the layout file is <route_name>_<controller_path>

app/code/BelVG/Showroom/view/frontend/layout/showroom_booking_index.xml


app/code/BelVG/Showroom/view/frontend/templates/showroom/booking/form.phtml

  1. Create database data and models

To create access to the database, data Models and Collection are used. Here is a base set of Models which are used for manipulating data.

app/code/BelVG/Showroom/Model/Showroom.php


app/code/BelVG/Showroom/Model/ResourceModel/Showroom.php


app/code/BelVG/Showroom/Model/ResourceModel/Showroom/Collection.php

<?php namespace BelVG\Showroom\Model\ResourceModel\Showroom; use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection; class Collection extends AbstractCollection


To add database data, InstallSchema and InstallData scripts are implemented.

InstallSchema is used for adding tables, fields, etc.

InstallData is used to add necessary content for the extension.

I used InstallData to add a list of showrooms.

app/code/BelVG/Showroom/Setup/InstallData.php


app/code/BelVG/Showroom/Setup/InstallSchema.php


Here is an example of Database tables:

modules-in-magento2-2

The admin part

The form will be accessed on the page http://<domain>/<backend_url>/showroom/booking/index

Here is an example of Grid:

modules-in-magento2-3

 

Here is an example of the Module’s configuration:

 

modules-in-magento2-4

 

There are two more ways that are often used to extend the existing methods in Magento 2 concepts: plugins and observers.

You can find more detailed information about plugins here. They could be used for every public method allowing you to add the code before or after the method or override the whole method.

Observers allow you to put the code inside the method using special events. You could use an already existing event or create your own one to extend your own methods when needed.

You can find more detailed information about observers here.

Debugging process

Magento should be turned to developer mode for the debugging process.

To configure Xdebug in PhpStorm use the guide.

To enable errors displaying two strings should be uncommented in app/bootstrap.php


To download the full version of the extension to see the implementation of the admin part

I hope that this article was helpful to you. Please feel free to share your opinion in the comments below!

Vlad Yunusov
Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Vlad

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *