Skip to main content
Blog General

How To Override a Controller In Magento 2?

January 19, 2016 |
eCommerce Business

Staying on M1 is not the right thing to do anymore. Get this done – Magento 1 to Magento 2 Migration NOW!

In Magento 2, the controller is responsible for handling incoming requests, and it’s the backbone of Magento routing implementation. 

When you are working on the custom modules in Magento 2, it’s very important to override core module files instead of making changes to it directly. Overriding the controller in Magento 2 gives you the flexibility to change the core functionality as per your Magento 2 module development requirements.

By following the steps given below you can override the core controller of Magento 2 easily. In this example, you will override the default 404 pages and display your custom 404 pages if the category URL is invalid.

The code is available in GitHub at

How To Override a Controller In Magento 2?

Step 1:

Create a custom module based on the Magento 2 structure in the folder app/code


Ex: Magento 2 root directory/app/code/DCKAP/Custom404Page/

Step 2:

Create a module.xml file in the below path to define your Magento 2 extension

Magento 2 root directory/app/code/DCKAP/Custom404Page/etc/module.xml


Step 3:

Create di.xml file in the below path to refer the overriding class

Magento 2 root directory/app/code/DCKAP/Custom404Page/etc/di.xml


Now DCKAPCustom404PageControllerCategoryView will be used to override


Migrate To Magento 2

Step 4:

Create routes.xml file in the below path for your custom controller file.

Magento 2 root directory/app/code/DCKAP/Custom404Page/etc/frontend/routes.xml


Step 5:

Create view.php file in the below path to define an overriding controller class

Magento 2 root directory/app/code/DCKAP/Custom404Page/Controller/Category


Step 6:

Create the custom controller file to show the custom 404 pages for the invalid category URL. Create the custom controller file in the following path…

Magento 2 root directory/app/code/DCKAP/Custom404Page/Controller/CategoryNoRoute/Index.php


Step 7:

Create a block file for the custom 404 pages in the following path…

Magento 2 root directory/app/code/DCKAP/Custom404Page/Block/CategoryNoRoute.php


Step 8:

Create layout file in the below path for the custom 404 page.

Magento 2 root directory/app/code/DCKAP/Custom404Page/view/frontend/layout/ custom404page_categorynoroute_index.xml


Step 9:

Create template file in the below path for the custom 404 page.

Magento 2 root directory/app/code/DCKAP/Custom404Page/view/frontend/templates/ categorynoroute.phtml


After completing the above steps, run the below Magento 2 upgrade comment in the command prompt.


After running the above comment , clear all the Magento cache and check on the Magento 2 admin side whether the extension installed successfully.

Check the browser whether the custom 404 controller is working fine, by providing a wrong category URL.


In the above URL, no category exists with the id 100.

Hope this blog helped you understand how to override controllers in Magento 2.

You might also be interested in our other Magento 2 blogs:

How to display Products in Magento 2 Homepage?Implementing SOAR & REST API in Magento 2Our Magento 2 Blogs of 2015Magento 2: Review and FeedbackA Sneak Peek into the Features of Magento 2



Balasubramaniam, aka Bala, is a Certified Magento Developer Plus Senior Software Engineer and one of our gems in the Magento team. A quick learner, he owns the projects that he works in and ensures they are delivered on time. He is very good at analyzing/fixing bugs and Magento customizations.

More posts by Balasubramaniam