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.
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
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0"?> /** * Bluethink Inc. * * @package Bluethink_SingleResponsbility * @author Bluethink <info@bluethink.in> */ <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Bluethink_SingleResponsbility" setup_version="0.0.1"/> </config> |
Step 2:
Create registration.php
app/code/Bluethink/SingleResponsbility/registration.php
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php /** * Bluethink Inc. * * @package Bluethink_SingleResponsbility * @author Bluethink <info@bluethink.in> */ \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Bluethink_SingleResponsbility', __DIR__ ); |
Step 3:
Create the routes.xml
app/code/Bluethink/SingleResponsbility/etc/frontend/routes.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version="1.0"?> /** * Bluethink Inc. * * @package Bluethink_SingleResponsbility * @author Bluethink <info@bluethink.in> */ <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd"> <router id="standard"> <route id="single" frontName="single"> <module name="Bluethink_SingleResponsbility" /> </route> </router> </config> |
Step 4:
Create layout file
single_index_index.xml
app/code/Bluethink/SingleResponsbility/view/frontend/layout/single_index_index.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0"?> <!-- /** * Bluethink Inc. * * @package Bluethink_SingleResponsbility * @author Bluethink <info@bluethink.in> */ --> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceContainer name="content"> <block name="single_responsibility_show" as="single_responsibility_show" template="Bluethink_SingleResponsbility::singleresponsibility.phtml"> <arguments> <argument name="notification" xsi:type="object">Bluethink\SingleResponsbility\ViewModel\Notification</argument> </arguments> </block> </referenceContainer> </body> </page> |
Step 5:
Now to create viewModel Notification.php
app/code/Bluethink/SingleResponsbility/ViewModel/Notification.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php /** * Bluethink Inc. * * * @category Bluethink * @package Bluethink_SingleResponsibility * @author Bluethink <info@bluethink.in> */ namespace Bluethink\SingleResponsbility\ViewModel; use Magento\Framework\View\Element\Block\ArgumentInterface; class Notification implements ArgumentInterface { public function getTitle() { return 'Single Responsbility from Bluethink'; } } |
Step 6: Create phtml file.
singleresponsibility.phtml
app/code/Bluethink/SingleResponsbility/view/frontend/templates/singleresponsibility.phtml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php /** * Bluethink Inc. * * * @category Bluethink * @package Bluethink_SingleResponsibility * @author Bluethink <info@bluethink.in> */ /** @var $viewModel \Bluethink\SingleResponsbility\ViewModel\ */ ?> <?php $viewModel = $block->getViewModel(); ?> <h1><?= $block->escapeHtml($viewModel->getTitle()); ?></h1> |
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
2022-07-27