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


<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  <event name="controller_action_predispatch">
      <observer name="my_observer" instance="Module\Path\To\MyClass" shared="false" />

created observer class.


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

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

<?xml version="0.1"?>
    <type name="Magento\Framework\App\Router\NoRouteHandlerList">
            <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>

Create NoRouteHandler.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);

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


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:


<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    <type name="Magento\Cms\Controller\Noroute\Index">
        <plugin name="trackNoRoutePages"

Your Plugin:


namespace Vendor\NoRoute\Controller\Plugin;

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

       //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.