I try to override a router, and I encounter some difficulties.

I try an override in the following way:

In my module2 DI : namespace2/module2/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="{Namespace1}\{module1}\Controller\Router" type="{Namespace2}\{module2}\Controller\Router" />

In my module2 frontend routes : namespace2/module2/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="{namespace2}">
            <module name="{namespace2}_{module2}" before="{namespace1}_{module1}" />

In my module2 router : namespace2/module2/Controller/Router

namespace Namespace2\Module2\Controller;

use Magento\Framework\App\RequestInterface;

class Router extends \Namespace1\Module1\Controller\Router
    public function __construct()

But after DI compile, my override not works.

Can you help me ? Thanks

1 Answer 1


You shouldn't create a rewrite config in di.xml file. It should be config scripts to add your controller router class to router list in Magento. It will work properly. For Example:

  • In di.xml file (It should be place in etc/frontend folder or etc/adminhtml folder if the uri is admin)

    <type name="Magento\Framework\App\RouterList">
            <argument name="routerList" xsi:type="array">
                <item name="router_name" xsi:type="array">
                    <item name="class" xsi:type="string">Namespace\Module\Controller\Router</item>
                    <item name="disable" xsi:type="boolean">false</item>
                    <item name="sortOrder" xsi:type="string">50</item>
  • Create Router class implements RouterInterface like this one:

    namespace Namespace\Module\Controller;
    class Router implements RouterInterface
        public function __construct() {
         * Match application action by request
         * @param RequestInterface $request
         * @return ActionInterface
        public function match(RequestInterface $request)
            // TODO: Implement match() method.

Your Answer

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

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