Single Responsibility in Magento 2

Blog
Spread the love

We are living in era of online e-commnerce market. For that I think Single Responsibility in Magento 2 will be best solution. We are living in era of online e-commnerce market. For that I think Magento will be best solution to retain all market aspects.

This Blog is totally depends on developer perspective. You must have heard about the SOLID Principle in Every Language.

The SOLID Principles are as follows:

  • Single Responsibility Principle
  • Open/Closed Principle
  • Liskov Substitution Principle
  • Interface Segregation Principle
  • Dependency Inversion Principle

You can go through with this blog of SOLID principle in php.

https://www.digitalocean.com/community/conceptual_articles/s-o-l-i-d-the-first-five-principles-of-object-oriented-design

Today I will discuss about the Single Responsibility principle in Magento 2.

Single Responsibility Principle:

According to definition

A class should have one and only one reason to change, meaning that a class should have only one job.

Every Class or Method should have responsibility with a single part.

Coming to Magento section

We have block classes to perform some custom view logic to show in phtml files.

In Block class we have to extend the class \Magento\Framework\View\Element\Template

Also for this parent class we are injecting so much dependencies that is unnecessary.

We can overcome from this problem with the help of view model.

Step 1:

Create Module.xml for your module.

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

Step 2:

Create registration.php

app/code/Bluethink/SingleResponsbility/registration.php

Step 3:

Create the routes.xml

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

Step 4:

Create layout file

single_index_index.xml

app/code/Bluethink/SingleResponsbility/view/frontend/layout/single_index_index.xml

Step 5:

Now to create viewModel Notification.php

app/code/Bluethink/SingleResponsbility/ViewModel/Notification.php

Step 6: Create phtml file.

singleresponsibility.phtml

app/code/Bluethink/SingleResponsbility/view/frontend/templates/singleresponsibility.phtml

It will help as we do not need to make block class.

That’s all From my side For Now.

Next we will continue with another property that is Open/Closed Principle.

bluethinkinc_blog

bluethinkinc_blog

2022-07-27

0

Leave a Reply

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

Find More Blogs

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

Google Ads Conversion Tracking Through Magento Platform:

Spread the love Google Ads Conversion Tracking Through

bluethinkinc Blogs