Toggle Nav
My Cart 0

Magento 2 redirect to 404 page from controller

Magento 2 redirect to 404 page from controller

Magento 2 redirect to 404 page from controller

If you want to display 404 for any specific controller or you want to redirect any page to the 404 page then you can use the below code.

This code is useful when you want to display a 404 page at the front-end when your custom module is disabled from the admin configuration.


 public function __construct(
       .......
        \Magento\Framework\Controller\Result\ForwardFactory $forwardFactory
    )
    {
       .......
        $this->_forwardFactory = $forwardFactory;
       .......
}
public function execute()
    {
        if (IS_MODULE_ENABLED))  {
            // Your code if module is enabled
        } else {
            $resultForward = $this->_forwardFactory->create();
            $resultForward->setController('index');
            $resultForward->forward('defaultNoRoute');
            return $resultForward;
        }
    }

NOTE: Tested on Magento CE 2.3.2

January 21, 2020
Did you like this post?
0
0