1
votes

I'm brand new to Magento 1.9 and am having trouble getting my config.xml file to load my layout file for the url: "localhost/index.php/moduleone/index/index". Can somebody help me figure out what I'm missing?

/app/code/local/Test/ModuleOne/etc/config.xml:

<config>
    <modules>
        <Test_ModuleOne>
            <version>0.1.0</version>
        </Test_ModuleOne>
    </modules>
    <frontend>
        <routers>
            ...
        </routers>
        <layout>
            <updates>
                <moduleone>
                    <file>moduleone.xml</file> <!-- Our layout file name-->
                </moduleone>
            </updates>
        </layout>
    </frontend>
    <global>
        ...
    </global>
</config>

/app/design/frontend/default/layout/moduleone.xml:

<?xml version="1.0"?>
    <layout version="0.1.0">
    <moduleone_index_index>
        <reference name="content">
            <block type="moduleone/moduleone" name="moduleone" template="moduleone/moduleone.phtml" />
        </reference>
    </moduleone_index_index>
</layout>
2
Enable and Use Frontend template Hints. You could use Magneto Debug from the Market. - anz
Make sure in the indexAction() of IndexController, you render the layout like below public function indexAction() { $this->loadLayout() ->renderLayout(); } - Manashvi Birla
Thanks guys. Both your comments were helpful. Turns out that there were a number of things that went wrong - the tutorial that I was following used the same name for modules, routes/urls, templates, blocks, etc. which made it very hard to understand how to "configure" Magento (it's like teaching someone multiplication by using the example 1 x 1 = 1). BTW - working with Magento feels more like configuring than programming... just my opinion. I posted the solution that worked for me below. - Vee

2 Answers

5
votes

It turns out that I had a number of items that were preventing Magneto from rendering the layout. Here's the recipe for getting a template and block rendered:

  1. In config.xml:

    • "Router Name" must a) match the "Router Name" component of layout tag, <mymodule_index_index> in "layout/mymodule.xml" and b) be unique (i.e - can't be used in another module).

    • <mymoduleurl> tag needs to match the "URL Name" (i.e. contents within the <frontName> tag).

    • <moduleoneblockname> is a "Block Name" needs to be defined in order to use the "type" attribute within the <block> tag in the layout file. Like the "Route Name", the "Block Name" needs to be unique.

/app/code/local/Test/ModuleOne/etc/config.xml:

<config>
    <modules>
        <Test_ModuleOne>
            <version>0.1.0</version>
        </Test_ModuleOne>
    </modules>
    <frontend>
        <routers>
            <mymodulerouter> <!-- Router Name -->
                <use>standard</use>
                <args>
                    <module>Test_ModuleOne</module> 
                    <frontName>mymoduleurl</frontName> <!-- Url Name -->
                </args>
            </mymodulerouter>
        </routers>
        <layout>
            <updates>
                <mymoduleurl> <!-- needs to match frontName tag -->
                    <file>modulelayoutfile.xml</file>
                </mymoduleurl>
            </updates>
        </layout>
    </frontend>
    <global>
        <blocks>
            <!-- new block definition -->
            <moduleoneblockname> <!-- Block Name -->
                <class>Test_ModuleOne_Block</class>
            </moduleoneblockname>
        </blocks>
    </global>
</config>
  1. Module file -

/app/etc/modules/Test_ModuleOne.xml file:

<config>
    <modules>
        <Test_ModuleOne>
            <active>true</active>
            <codePool>local</codePool>
        </Test_ModuleOne>       
    </modules>
</config>
  1. Controller -

/app/code/local/Test/ModuleOne/controllers/IndexController.php:

<?php
    class Test_ModuleOne_IndexController extends Mage_Core_Controller_Front_Action{
        public function indexAction(){
            $this->loadLayout(); // loads the default layout file, page.xml
            $this->renderLayout(); // renders the default layout file, page.xml
        }
    }
  1. Layout file (NOTE: files stored in the /app/design/frontend/base path will be overwritten when Magento is upgraded!)

    • "Router Name" component of the <mymodulerouter_index_index> tag should match the "Router Name" specified in the config.xml file.
    • "type" attribute follows the format: type="block name"/"block class". "Block name" is defined as a tag in config.xml (within the <blocks> tag) while "block class" is defined in a class php file in the "Block" directory. In this example, "Block Name" is defined in the <moduleoneblockname> tag in config.xml and "Block Class" is defined in the file, "Block/myblockclass.php" (see item #5)

/app/design/frontend/base/default/layout/modulelayoutfile.xml:

<layout version="0.1.0">
    <mymodulerouter_index_index> <!-- Format of tag is: (Router Name)_(controller name)_(controller action)-->
        <reference name="content">
            <block type="moduleoneblockname/myblockclass" name="right" template="mytemplate/mytemplate.phtml"/>
        </reference>
    </mymodulerouter_index_index>
</layout>
  1. Block class file
    • The "myblockclass" component of the class name, "Test_ModuleOne_Block_myblockclass", must be used as the name of the file.

/app/code/local/Test/ModuleOne/Block/myblockclass.php:

<?php
class Test_ModuleOne_Block_myblockclass extends Mage_Core_Block_Template
{
    public function myfunction()
    {
        return "<br>Loaded myblockclass.php class";
    }
}
  1. Template file: (NOTE: files stored in the /app/design/frontend/base path will be overwritten when Magento is upgraded!)

/app/design/frontend/base/default/template/mytemplate/mytemplate.phtml

<?php
    echo "In mytemplate.phtml";
    echo $this->myfunction();
?>
0
votes

it is often routes wont trigger when you put

                <module>
                  Packt_Helloworld
                </module>
                <frontName>
                  helloworld
                </frontName>

you should do it inline, otherwise route will not work properly :

                <module>Packt_Helloworld</module>
                <frontName>helloworld</frontName>

maybe this could help someone