I am using a third party module, but it doesn't fulfill all the requirements. So I have to do some customizations in this module. If I do changes directly in the third party module then it will be overridden when we update the module.

What is the best way to override this third-party module?

I have to override fronted controller file.

I am new to Magento. Please provide me some guidance.

3 Answers 3


You can override functionality using any of the following methods depending on your requirement.

You can go through https://inchoo.net/magento-2/overriding-classes-magento-2/ for more details on which method to use according to your requirement.


You need to follow these steps to Overriding a controller in Magento 2:

Step 1: Create file di.xml file in app/code/[Name space]/[Your Module]/etc

Create file di.xml file in app/code/[Name space]/[Your Module]/etc

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
   <preference for="Magento\Cms\Controller\Index\Index" 
               type="[Name Space]\[Your Module]\Controller\Cms\Index" />

This example You want to rewrite controller Like Magento/Cms/Controller/Index/Index

Step 2 – Defining an overriding controller class
Create Index.php in app/code/[Name Space]/[Your Module]/Controller/Index

namespace [Name Space]\[Your Module]\Controller\Index;  

class Index extends \Magento\Cms\Controller\Index\Index  
    public function execute()  
        // todo  

Step 3: Done!
Thank you!


Anshu & Jugal's answers are definitely the official way to do things, but there's also a popular method for people who use composer for integrating Magento. If the file you want to modify is within the composer vendor/ directory then you can use a package called cweagans/composer-patches to create patches on specific files.

The benefit of this method is that it helps with upgradeability. It will patch the file after each composer install or composer update and if it can't apply the patch anymore (if the module was updated, for example) then the patch will fail and it'll tell you about it so you know to modify/remove the patch for the new version.

See here for the actual package and it's usage: https://github.com/cweagans/composer-patches

Basic steps are:

  • require the package cweagans/composer-patches in your project
  • Create a patch list file. e.g. composer.patches.json
  • Point to the patch list file in composer.json
  • Generate your patch from a diff
    • You can generate one using diff -u old/file.ext new/file.ext > thediff.patch
  • Make sure in your generated patch that the root of the file is relative to the package's root
    • i.e. if your file is at vendor/magento/module-catalog-search/etc/search_request.xml then the top lines would be etc/search_request.xml. See the example below.
  • In your patch list file, list your patch files against what module you're patching, with a description.

This example below was a fix for the magento/module-catalog-search module where I modified an XML file. It can be any text file, no matter if it's PHP/PHTML, JS, XML, CSS, etc...

File: {magento_root}/composer.json

    "require": {
        "cweagans/composer-patches": "^1.6"
    "extra": {
        "patches-file": "composer.patches.json"

File: {magento_root}/composer.patches.json

  "patches": {
    "magento/module-catalog-search": {
      "Change Magento search criteria rule from \"should\" to \"must\"": "patches/search-and_to_or.patch"

File: {magento_root}/patches/search-and_to_or.patch

--- etc/search_request.xml      2019-09-30 17:01:46.914871669 +0100
+++ etc/search_request.xml      2019-09-30 17:01:57.887042180 +0100
@@ -13,7 +13,7 @@
             <query xsi:type="boolQuery" name="quick_search_container" boost="1">
-                <queryReference clause="should" ref="search" />
+                <queryReference clause="must" ref="search" />
                 <queryReference clause="must" ref="category"/>
                 <queryReference clause="must" ref="price"/>
                 <queryReference clause="must" ref="visibility"/>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.