Magento 1.9 – Custom Module Controller Overwriting Default Layout

controllerscustomlayoutmagento-1.9

I made a custom controller and declared a layout here.
When I go to mysite/custom the controller and the layout works fine.
But when I access the indexsite (mysite/index) the layout which I used for the Custom_IndexController is also loaded.

I just want this layout to be loaded on the specific view.
Please let me know what I'm doing wrong here.

This is what my config.xml looks like

<?xml version="1.0"?>
<config>
    <modules>
        <Unreal_Custom>
            <version>0.1.0</version> 
        </Unreal_Custom>
    </modules>
    <frontend>
        <routers>
            <custom>
                <use>standard</use>
                <args>
                    <module>Unreal_Custom</module>
                    <frontName>custom</frontName>
                </args>
            </custom>
        </routers>

        <layout>
          <updates>
            <custom>
              <file>uecustom.xml</file>
            </custom>
          </updates>
        </layout>
    </frontend>
</config>

My Controller:

<?php
class Unreal_Custom_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        echo "foo";
        $this->loadLayout();
        $this->renderLayout();
    }
}

The corresponding layout

<layout version="0.1.0">
    <default module="Unreal">

        <block type="page/html" name="root" output="toHtml" template="unreal/custom/main.phtml" >           
            <block type="page/html_head" name="header" as="header" template="unreal/custom/header.phtml"></block>
            <block type="page/html" name="content" as="content" template="unreal/custom/content.phtml"></block>
            <block type="page/html_footer" name="footer" as="footer" template="unreal/custom/footer.phtml"></block>
        </block>

    </default>
</layout>

Best Answer

The "handle" in your layout file should be custom_index_index.

These handles consist of:

  1. Module name (defined/used in config.xml layout updates section)
  2. The name of the controller, same as used in the file and class name (in your case Custom_IndexController => "index"
  3. The action name, same as the method for handling the action (in your case, the default indexAction() => "index"

So fully assembled, this would be custom + index + index = custom_index_index

<layout version="0.1.0">
    <custom_index_index module="Unreal">

        <block ... >            
            <!-- ... -->
        </block>

    </custom_index_index>
</layout>

If you would use the default handle, then these blocks / actions / etc.. would be applied on every page, by "default" ;)

Related Topic