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

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

Overriding Block, Model, Controller In Magento2

As we know that Magento 2 had come out with all new features. Which helped Magento 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.



    
  

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:

    

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.


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.

  


    
 

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


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



    

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


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

  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn
  • Youtube

leave a comment