How to Override or Rewrite Controller in Magento 2
Vinh Jacker | 12-18-2024
Updating a feature in Magento can sometimes pose challenges due to the complexities of the application’s core functions. However, customizing Magento often involves overriding controllers to ensure they match specific requirements. The good news is there’s a simpler approach available that can save you time and effort in this article. We’ll guide you through the steps needed to overwrite or rewrite a controller in Magento 2, enabling you to adjust the platform to your exact needs easily.
How to Override Controller in Magento 2
Step 1: Create the custom module
The first step is to create a new module in the app/code
directory for your Magento 2 installation. For instance, you can name your module “Custom_Module”.
# Replace Vendor_Module with your actual vendor and module name
mkdir -p app/code/Vendor/ModuleName
Step 2: Register the module
The next step is to create a registration.php
file to declare your module.
# app/code/Vendor/ModuleName/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_ModuleName',
__DIR__
);
Step 3: Create module configuration
After completing these steps above, you should define the module configuration by creating the module.xml
file.
# app/code/Vendor/ModuleName/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_ModuleName" setup_version="1.0.0"/>
</config>
Step 4: Create a controller class
To override an original controller, you should extend it by creating a custom controller class.
# app/code/Vendor/ModuleName/Controller/Path/To/OriginalController.php
<?php
namespace Vendor\ModuleName\Controller\Path\To;
class OriginalController extends \Magento\Catalog\Controller\Category\View
{
// Your custom controller logic here
}
Step 5: Configure the dependency injection
It is time to configure the dependency injection. Thus, you need to create a di.xml
file.
# app/code/Vendor/ModuleName/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Controller\Category\View" type="Vendor\ModuleName\Controller\Path\To\OriginalController" />
</config>
Step 6: Create a routes.xml file
Then, it is necessary to configure the custom route by creating the routes.xml
file.
# app/code/Vendor/ModuleName/etc/frontend/routes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="custom" frontName="custom">
<module name="Vendor_ModuleName"/>
</route>
</router>
</config>
Step 7: Verify and enable the module
Finally, you should enable your module and clear the cache.
Executing the following commands from the root directory of your Magento 2 installation:
bin/magento module:enable Vendor_ModuleName
bin/magento setup:upgrade
bin/magento cache:flush
Your custom controller should now be active, and you can access it via the custom route, such as:
http://yourmagento.com/custom/path/to/originalcontroller
Ensure you replace Vendor_ModuleName
with your actual vendor & module names and Path\To
with the correct path to the original controller. Besides, adjust the controller logic to meet your specific needs.
How to Rewrite Controller in Magento 2
In Magento 2, controllers can be rewritten without using preferences by configuring custom routing. This method requires creating a new controller and specifying its path in the routes.xml
file.
Step 1: Create a new controller
Firstly, you need to create a new controller file for your custom module.The controller should be place it at app/code/[Vendor]/[Module]/Controller/[Controller]/[Action].php
. Replace [Vendor], [Module], [Controller], and [Action]
with your specific values.
For instance, to override the Index
controller’s execute
action from Magento_Catalog
, you need to create the file as shown.
// app/code/[Vendor]/[Module]/Controller/Index/Index.php
namespace [Vendor]\[Module]\Controller\Index;
class Index extends \Magento\Catalog\Controller\Category\View
{
public function execute()
{
// Your custom code here
}
}
Step 2: Create a routes.xml file
To define a new frontend route, you need to create a routes.xml file in app/code/[Vendor]/[Module]/etc/frontend.xml. This file will specify the new route and map it to the correct controller.
<!-- app/code/[Vendor]/[Module]/etc/frontend/routes.xml -->
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="[route_id]" frontName="[front_name]">
<module name="[Vendor]_[Module]" before="[Magento_Module]" />
</route>
</router>
</config>
It is necessary to replace [route_id]
, [front_name]
, [Vendor]
, [Module]
, and [Magento_Module]
with specific values.
Step 3: Update the controller in routes.xml
By using the before
attribute in the <module>
node of routes.xml, you can easily identify the original controller to override.
Step 4: Clear the cache
After making your changes, run the command php bin/magento cache:clean
to clear the cache and apply your modifications.
When you complete these steps, your custom controller will now be triggered instead of the original controller.
You can access it using the URL: http://yourmagento.com/hello/account\create
If you got this error message: Exception printing is disabled by default for security reasons, this topic may help.
In conclusion
This how to override or rewrite controller in Magento 2 guide provides a structured approach to rewriting or overriding a controller in Magento 2, ensuring that your custom logic is implemented correctly. Remember to test your changes thoroughly in a development environment before deploying to production. If you need further assistance or have specific questions, feel free to ask!
Related Post