Magento 2 Custom No Route Handler redirect to default 404 Not Found Page
Question
I am trying to redirect url to my custom no route found page but it always redirect to default 404 page,
I have made below changes in my module
Step-1 Create below file in Controller Folder CustomNoRouteHandler.php
<?php
namespace SimplifiedMagento\RequestFlow\Controller\Page;
class CustonNoRouteHandler implements \Magento\Framework\App\Router\NoRouteHandlerInterface
{
public function process(\Magento\Framework\App\RequestInterface $request)
{
$request->setRouteName('noroutefound')->setControllerName('page')->setActionName('customnoroute');
return true;
}
}
Step-2 My redirect page which should be display while no route found,
<?php
namespace SimplifiedMagento\RequestFlow\Controller\Page;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\ResponseInterface;
class CustomNoRoute extends Action
{
public function execute()
{
echo "this is our custom 404 page";
}
}
Step-3 In etc/frontend/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\Framework\App\Router\NoRouterHandlerList">
<arguments>
<argument name="handlerClassList" xsi:type="array">
<item name="handlerinfo" xsi:type="array">
<item name="class" xsi:type="string">SimplifiedMagento\RequestFlow\Controller\Page\CustonNoRouteHandler</item>
<item name="sortOrder" xsi:type="string">50</item>
</item>
</argument>
</arguments>
</type>
</config>
Step-4 In etc/frontend/routes.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="noroutefound" frontName="noroutefound">
<module name="SimplifiedMagento_RequestFlow" />
</route>
</router>
</config>
No correct solution
OTHER TIPS
I have done the same custom 404 page for one of my extension.
I see a difference of code in Route php file process method.
Your ones is
$request->setRouteName('noroutefound')->setControllerName('page')->setActionName('customnoroute');
While i found in my code is
$request->setModuleName('noroutefound')->setControllerName('page')->setActionName('customnoroute');
Also put di.xml inside etc/di.xml insetad of etc/frontend/di.xml
Try this change once if that work for you.
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange