2

How to check action noRoute (404) on every page using Observer?

etc/events.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
  <event name="controller_action_predispatch">
      <observer name="my_observer" instance="Module\Path\To\MyClass" shared="false" />
  </event>
</config>

created observer class.

<?php 

namespace Module\Namespace\Path;

use Magento\Framework\Event\ObserverInterface;

class MyClass implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
   {
       $request = $observer->getEvent()->getControllerAction()->getRequest();
        $actionName = $request->getActionName();
       echo $actionName; // 404 page not gettig "noRoute"
   }

}
  • You want to track the 404 not found page? – Khoa TruongDinh Mar 1 '17 at 14:28
  • yes.I want getActionName() while 404..in magento1 get "noRoute" but in magento2 not this. – Magento2 Devloper Mar 1 '17 at 14:30
  • AFAIK, If using rewrite url, you cannot get the module. – Khoa TruongDinh Mar 1 '17 at 14:47
  • ok but getActionName(); method give me 'index' in 404.. – Magento2 Devloper Mar 1 '17 at 14:59
3

I have added a custom no route handler using the following code

<?xml version="0.1"?>
<config>
    <type name="Magento\Framework\App\Router\NoRouteHandlerList">
        <arguments>
            <argument name="handlerClassesList" xsi:type="array">
                <item name="hello" xsi:type="array">
                    <item name="class" xsi:type="string">Smartie\HelloRouter\App\Router\NoRouteHandler</item>
                    <item name="sortOrder" xsi:type="string">10</item>
                </item>
            </argument>
        </arguments>
    </type>
</config>

Create NoRouteHandler.Php

<?php

namespace Smartie\HelloRouter\App\Router;

class NoRouteHandler implements \Magento\Framework\App\Router\NoRouteHandlerInterface
{
    public function process(\Magento\Framework\App\RequestInterface $request)
    {
        $this->_response->setRedirect('http://magento.stackexchange.com/questions', 301);
        $this->_response->sendResponse();
    }
}

It's Worked..but some of the category and products pages not find action of modules.

1

Forgot Observer, should use Plugin as well as possible. Because, your observer will be called each time when making a request. So, it it not good for performance.

If we want to track the 404 not found page only, you should follow below way.

On the front page, the "noRoute" will use the controller: vendor/magento/module-cms/Controller/Noroute/Index.php

We should use Plugin:

For example:

app/code/Vendor/NoRoute/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">

    <type name="Magento\Cms\Controller\Noroute\Index">
        <plugin name="trackNoRoutePages"
                type="Vendor\NoRoute\Controller\Plugin\TrackNoRoutePages"/>
    </type>
</config>

Your Plugin:

<?php

namespace Vendor\NoRoute\Controller\Plugin;

class TrackNoRoutePages
{
    public function afterExecute(
        \Magento\Cms\Controller\Noroute\Index $subject,
        $return
    ) {

       //Your code here
        return $return;
    }
}

NOTE: If category and product are using url rewrite, seem that we cannot get the module name

  • Ok..that's Right answer..but I need in Observer..above my answer also worked for me. – Magento2 Devloper Mar 1 '17 at 15:10
  • @Magento2Devloper forgot Observer, should use Plugin. Because, your observer will be called each time when making a request. So, it it not good for performance. – Khoa TruongDinh Mar 1 '17 at 15:18
  • ok Thanx i will check it. – Magento2 Devloper Mar 1 '17 at 15:20
  • @Magento2Devloper yeah! check it and let me know. – Khoa TruongDinh Mar 2 '17 at 0:21
  • Thanx.but it didn't work for me I need category and products pages also..any idea about it? – Magento2 Devloper Mar 2 '17 at 6:17

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.