3

I have used below code in di.xml

<type name="Magento\Framework\App\RouterList">
    <arguments>
        <argument name="routerList" xsi:type="array">
            <item name="customrouter" xsi:type="array">
                <item name="class" xsi:type="string">Custom\Catalog\Controller\Router</item>
                <item name="disable" xsi:type="boolean">false</item>
                <item name="sortOrder" xsi:type="string">50</item>
            </item>
        </argument>
    </arguments>
</type>

But i am not able to call the funtion Custom\Catalog\Controller\Router also to tried to exit the code but unable to reach given controller. Can any one give me solution

3

3 Answers 3

7

Using "Magento\Framework\App\RouterList" we use the di.xml file in our module.

app/code/Vendor/Module/etc/di.xml

<type name="Magento\Framework\App\RouterList">
      <arguments>
          <argument name="routerList" xsi:type="array">
              <item name="customrouter" xsi:type="array">
                  <item name="class" xsi:type="string">Vendor\Module\Controller\CustomRouter</item>
                  <item name="disable" xsi:type="boolean">false</item>
                  <item name="sortOrder" xsi:type="string">22</item>
              </item>
          </argument>
      </arguments>
  </type>

After that we need to create a CustomRouter class.

app/code/Vendor/Module/Controller/CustomRouter.php :

namespace Vendor\Module\Controller;
class CustomRouter implements \Magento\Framework\App\RouterInterface
{
   protected $actionFactory;
   protected $_response;
   public function __construct(
       \Magento\Framework\App\ActionFactory $actionFactory,
       \Magento\Framework\App\ResponseInterface $response
   ) {
       $this->actionFactory = $actionFactory;
       $this->_response = $response;
   }
   public function match(\Magento\Framework\App\RequestInterface $request)
   {
       $identifier = trim($request->getPathInfo(), '/');
       if(strpos($identifier, 'customrouter') !== false) {
       $request->setModuleName('customrouter')-> //module name
       setControllerName('index')-> //controller name
       setActionName('index')-> //action name
       setParam('param', 3); //custom parameters
       } else {
           return false;
       }
       return $this->actionFactory->create(
           'Magento\Framework\App\Action\Forward',
           ['request' => $request]
       );
   }
}

And finally you need to create a routes.xml file. app/code/Vendor/Module/etc/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="customrouter" frontName="customrouter">
          <module name="Vendor_Module" />
      </route>
  </router>
</config>
1

Try editing/creating the file at /etc/frontend/di.xml

Notice the "frontend" in the path

I had this issue and the developer docs didn't make it clear. When I consulted another module, Magento's CMS implementation, I saw this: https://github.com/magento/magento2/blob/2.3/app/code/Magento/Cms/etc/frontend/di.xml

0

Frontend route

Routes.xml To register a frontend route, we must create a routes.xml file:

File: app/code/Mageplaza/HelloWorld/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">
<!--Use router 'standard' for frontend route-->
<router id="standard">
    <!--Define a custom route with id and frontName-->
    <route frontName="helloworld" id="helloworld">
        <!--The module which this route match to-->
        <module name="Mageplaza_HelloWorld"/>
    </route>
</router>
</config>

Please look into the code, you will see it’s very simple to register a route. You must use the standard router for the frontend. This route will have a child which define the module for it and 2 attributes:

The id attribute is a unique string which will identify this route. You will use this string to declare the layout handle for the action of this module The frontName attribute is also a unique string which will be shown on the url request. For example, if you declare a route like this:

Admin route

This route will be same as the frontend route but you must declare it in adminhtml folder with router id is admin.

File: app/code/Mageplaza/HelloWorld/etc/adminhtml/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">
<!--Use router 'admin' for admin route -->
<router id="admin">
    <!--Define a custom route with id and frontName -->
    <route id="mageplaza_helloworld" frontName="mageplaza_helloworld">
        <!--The module which this route match to-->
        <module name="Mageplaza_HelloWorld"/>
    </route>
</router>
</config>

The url of the admin page is the same structure with frontend page, but the admin_area name will be added before route_frontName to recognize this is a admin router. For example, the url of admin cms page:

http:// example.com/index.php/admin/mageplaza_helloworld/controller/action

For Reference: Magento 2 Routing

2

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.