1

I'm creating a module to use custom url rewrites. For this I have created a module and I am using a custom UrlRewrite Controller Router.

For testing purpose, I generated the Router below. It works perfectly on handlers like CMS pages and Categories Pages ('hello world' is displayed), but it is not called in the search pages, as if it did not exist.

<?php
namespace Mycompany\Shopby\Controller;

use Magento\Framework\UrlInterface;
use Magento\UrlRewrite\Model\UrlFinderInterface;
use Mycompany\Shopby\Model\Url\Hydrator;

/**
* Class Router
* @package Mycompany_Shopby
*/
class Router extends \Magento\UrlRewrite\Controller\Router implements \Magento\Framework\App\RouterInterface
{

    [ ... consts, protected var, constructor ... ]

    /**
    * Match corresponding navigation URL and modify request
    *
    * @param \Magento\Framework\App\RequestInterface $request
    * @return \Magento\Framework\App\ActionInterface|null
    */
    public function match(\Magento\Framework\App\RequestInterface $request)
    {
        var_dump('hello world');
        die;
    }

Soon on pages like www.mysite.com/category1, hello world is displayed. But on pages like www.mysite.com/catalogsearch/result?q=product, my Router is not called.

In my di.xml I have the following definition of RouterList.

<type name="Magento\Framework\App\RouterList">
    <arguments>
        <argument name="routerList" xsi:type="array">
            <item name="navigationRoute" xsi:type="array">
                <item name="class" xsi:type="string">Mycompany\Shopby\Controller\Router</item>
                <item name="disable" xsi:type="boolean">false</item>
                <item name="sortOrder" xsi:type="string">30</item>
            </item>
        </argument>
    </arguments>
</type>

What do I need to do to get my router to call other handlers like catalogsearch_result_index?

  • What is logic of custom routers? on which URL what URL hit var_dump('hello world'); die; ios not goood – Amit Bera Oct 26 '18 at 14:52
  • @AmitBera I'll use custom routers to rewrite layered navigation urls, like this: category?manufacturer=ferrari will be category/manufacturer/ferrari. How I said, var_dump('hello world'); die; is just for testing purpose. My custom routers current works as well on catalog_category_view handler, but it's not called into catalogsearch_result_index handler and I want this, so I tested it with var_dump and die. – Denis Spalenza Oct 26 '18 at 15:39
0

The problem is the name of router item. I changed navigationRoute to default on attribute name.

<type name="Magento\Framework\App\RouterList">
    <arguments>
        <argument name="routerList" xsi:type="array">
            <item name="default" xsi:type="array">
                <item name="class" xsi:type="string">Intercase\Shopby\Controller\Router</item>
                <item name="disable" xsi:type="boolean">false</item>
                <item name="sortOrder" xsi:type="string">100</item>
            </item>
        </argument>
    </arguments>
</type>

navigationRoute are a custom route, but I was looking for a a router to work with native routes too. So I changed to default and resolved my problem.

Knowledge Source: https://devdocs.magento.com/guides/v2.1/extension-dev-guide/routing.html https://belvg.com/blog/how-to-create-custom-router-in-magento-2.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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