There are two ways to display 404 page in Magento.
1. We can call default CMS page of 404 which is already created in back-end.
2. We can call the noroute.phtml file for 404
Sometimes, the module is disabled or it does not display any content in the site as per below screen shot.
and displays the blank page, which doesn’t look good and is not a good impression.
So a better solution is to display a 404 page.
Lets start How we can add the 404 code.
We can call default CMS page of 404 which is already created in back-end.
1. Add this code in the controller file, if module is disabled or the condition is not fulfilled.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public function execute() { $resultPage = $this->_scopeConfig->getValue('webmull/general/active', \Magento\Store\Model\ScopeInterface::SCOPE_STORE); if (!$resultPage) { // Module is disabled /** @var \Magento\Framework\Controller\Result\Forward $resultForward */ $resultForward = $this->resultForwardFactory->create(); $resultForward->forward('noroute'); // call the no route method return $resultForward; }else{ /*Module is enabled */ $this->_view->loadLayout(); $this->_view->getLayout()->initMessages(); $this->_view->renderLayout(); } } |
We can call the noroute.phtml file to display a 404 page.
1. Add this code in the controller file, if module is disabled or the condition is not fulfilled.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public function execute() { $resultPage = $this->_scopeConfig->getValue('webmull/general/active', \Magento\Store\Model\ScopeInterface::SCOPE_STORE); if (!$resultPage) { // Module is disabled /** @var \Magento\Framework\Controller\Result\Forward $resultForward */ $resultForward = $this->resultForwardFactory->create(); $resultForward->forward('defaultNoRoute'); // Call the another action of your module return $resultForward; }else{ /*Module is enabled */ $this->_view->loadLayout(); $this->_view->getLayout()->initMessages(); $this->_view->renderLayout(); } } |
2. Create the defaultNoRoute.php file in the path app/code/Webmull/{Module Name}/Controller/Index;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
namespace Webmull\{Module Name}\Controller\Index; class DefaultNoRoute extends \Magento\Framework\App\Action\Action { protected $resultPageFactory; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory ) { $this->resultPageFactory = $resultPageFactory; parent::__construct($context); } public function execute() { $resultLayout = $this->resultPageFactory->create(); $resultLayout->setStatusHeader(404, '1.1', 'Not Found'); $resultLayout->setHeader('Status', '404 File not found'); return $resultLayout; } } |
3. Create the {Module Name}_index_defaultnoroute.xml in app/code/{package Name}/{Module Name}/view/frontend/layout
1 2 3 4 5 6 7 8 9 |
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceContainer name="content"> <block class="Magento\Framework\View\Element\Template" name="default_no_route" template="Magento_Cms::default/no-route.phtml"/> </referenceContainer> </body> </page> |
Instead of this “Magento_Cms::default/no-route.phtml” default file, We can use this phtml file to display a 404 page.
And now refresh the page of your module to see the effect.