How to Override Block, Model, Controller In Magento 2 ?


Posted February 13, 2019 by eglobeits

let's see how Magento Development company use override block, model, controller.

 
As we know that Magento 2 had come out with all new features. Which helped developers to implement core files bit easier compared to before.

From the developers perspective, while developing a Magento site we need to customize some core files for its proper functioning. In those cases, we will have to override the core files instead of making direct changes.

It is not recommended to modify core files in Magento 2.

Here we use customization which involves adding new features or the overriding existing one.

If we have compared Magento 2 with the old, we may notice that Magento 2 comes with some new concept of dependencies injection which helps you inject classes dependencies for an object which makes overriding pretty easier.

Here we can use di.xml file configuration through which dependencies are injected by the object manager and each module can have the global and area-specific di.xml file that can be used depending on scope.

Now you might have a small idea, let's see how Magento Development company use override block, model, controller.

1. Overriding Block

Let's override Magento 2 Catalog Product ListProduct Block.

Step 1 : First of all we have to override di.xml file in app/code/{vendor_name}/{module_name}/etc.

For example app/code/Egits/Customcatalog/etc and add the following code in it.

code
source






In preference for=" " add the block which you want to override and in preference type=" " add our custom block where to override it.

Step 2 : Now override ListProduct.php

Create ListProduct.php in app/code/Egits/Customcatalog/Block/Product and add this code in it:

code
source



Here, For example, some of you might want to add some additional data in the product listing page that is to add a timer in product listing page in those cases then you can override ListProduct block to add custom functionalities.

code
source








Here we added our custom function getTimer() in ListProduct block. The function simply returns the string in variable $time. And we need to call the function in our list.phtml file that we have overridden.

Now the block is overridden. You can use the same method for overriding any block in Magento 2.



2. Overriding Magento 2 model

Let's override Catalog Product model

Step 1 : Create di.xml file in app/code/Egits/Customcatalog/etc to specify which model to override.



code
source








Step 2 : Now Create Product.php in app/code/Egits/Customcatalog/Model/Catalog

code
source





In this code, we have overridden the Product SKU.



3. Overriding Controller

Lets override Product View controller in magento2

Step 1 : Just like models and blocks override di.xml in app/code/Egits/Customcatalog/etc



code
source






Step 2 : Now create View.php controller file in app/code/Egits/Customcatalog/Controller/Product

code
source





You can use the same method for overriding any controller in magento2.

After these steps run the following commands in your command prompt.

sudo php bin/magento setup:upgrade

sudo php bin/magento setup:di:compile

sudo php bin/magento setup:static-content:deploy

sudo php bin/magento cache:flush
-- END ---
Share Facebook Twitter
Print Friendly and PDF DisclaimerReport Abuse
Contact Email [email protected]
Issued By eGlobe IT Solutions
Country India
Categories Software , Technology , Web Development
Last Updated February 13, 2019