Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento how to trigger a 404 redirect from controller when extension overwrite root layout node

Tags:

layout

magento

I've written an extension for Magento which will initiate a 404 redirect from the controller under certain circumstances. The code in the controller that causes the 404 redirect is:

$this->getResponse()->setHeader('HTTP/1.1','404 Not Found');
$this->getResponse()->setHeader('Status','404 File not found');
$pageId = Mage::getStoreConfig('web/default/cms_no_route');
if (!Mage::helper('cms/page')->renderPage($this, $pageId)) {
    $this->_forward('defaultNoRoute');
}

This code is accessed via the URL www.mysite.com/mymodule/index/action?name=value and the redirect is triggered based on 'value'.

However, because because my module contains a layout update which overwrites the root node:

<layout version="0.1.0">
    <mynamespace_mymodule_index_action>
        <block type="mymodule/list" name="root" template="mynamespace/mymodule/list.phtml" output="toHtml" />
    </mynamespace_mymodule_index_action>
</layout>

the 404 redirect is showing a blank (white) page, which contains the following HTML:

<head>
</head>
<body>
    <div id="outerdivshade">    
        <div id="outerdivpadding">
            <div class="wrapper">
                <div class="page">
                    <div class="main-container col2-left-layout">
                        <div class="main">
                            <div class="col-main"></div>
                            <div class="col-left sidebar"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>

I'm guessing that by updating of the root node I've left a layout structure that the redirect code cannot work with to create the complete 404 page.

By removing the if statement and leaving just the code:

$this->_forward('defaultNoRoute');

everything works, but I don't understand the pros and cons of doing this.

Interestingly the above code works correctly on Magento 1.6, but not on 1.7.

I'd appreciate any comments on the right way of including a working 404 redirect, but still allowing me to use my own root node in the layout.

Thanks

like image 782
Dom Avatar asked Dec 22 '13 12:12

Dom


1 Answers

For showing the 404 page you can call the norouteAction of the Mage_Core_Controller_Varien_Action class.

$this->norouteAction();
return;

The 404 page can be edited in the backend. The page has the title 404 Not Found 1. You can also put in there your custom layout update XML.

like image 68
smiggle Avatar answered Oct 26 '22 09:10

smiggle