How to Override Block, Model, Controller In Magento 2

Got stuck in overriding core model Blocks and controllers in Magento2?
no worries!!
read our entire blog to fix it

Here is the 3 different steps you follow

In Magento 2, the controller is subject to controlling incoming requests, and it’s the resolution of Magento routing implementation.

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

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.
When you are going on the custom modules in Magento 2, it’s really necessary to override core module files alternatively of doing modifications to it directly.
Overriding the controller in Magento 2 provides you the flexibility to modify the core functionality as per your Magento 2 module development demands.
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. Override Block in Magento 2

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. Override model in Magento 2

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. Override Controller in Magento 2

Lets override Product View controller in magento2

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

<?xml version="1.0"?-->

    

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

<?php 

namespace Egits\Customcatalog\Controller\Product;

class View extends \Magento\Catalog\Controller\Product\View
{
    /**
     * @return \Magento\Framework\Controller\Result\Redirect|\Magento\Framework\View\Result\Page
     */
    public function execute()
    {
        // Place your code here
        return parent::execute();
    }
}
?-->

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

Leave a Reply

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