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 default 404 page and display your custom 404 page if the category URL is invalid.

The code is available in GitHub at https://goo.gl/FJHpL3

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

Img_1

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

Img_2

Step 3:

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

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

Img_3

Now DCKAP\Custom404Page\Controller\Category\View will be used to override

Magento\Catalog\Controller\Category\View

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

Img_4

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

Img_5

Step 6:

Create the custom controller file to show the custom 404 page 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

Img_6

Step 7:

Create block file for the custom 404 page in the following path…

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

Img_7

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

Img_8

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

Img_9

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

Img_10

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.

Example:

http://www.yourdomain.com/catalog/category/view/id/100

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 2
Our Magento 2 Blogs of 2015
Magento 2: Review and Feedback
A Sneak Peek into the Features of Magento 2

 

Be the first to Know

Subscribe to our blog to get the latest articles directly to your inbox.

READY TO DRIVE SALES?

Request a Quote Today

Request a Quote

Leave a Reply

1 Comment on "How To Override a Controller In Magento 2?"


admin
Admin
2 years 3 months ago

Hi,
Thanks for your comments.
We are happy that the blog helped.
We will be publishing a lot more blogs on M2 soon.
Keep reading. Thanks again.