We will continue on the same module developed last time at http://magentoexpertforum.com/showth...-1-Get-started
There are few changes to make to the existing config.xml file. Since now we are using layouts, we will create our own layout file for our module. Lets name our module’s layout file as test.xml. We will place it in our theme/layout folder.
Now create the test.xml file in your theme layout folder. Like in my case its app/design/frontend/default/default/layout/test.xml or if you have create your own theme, put the layout file there. Here are the contents of the file.
<layout> <!-- New Section Added -->
<file>test.xml</file> <!-- This is name of the layout file for this module -->
The explanation of this layout file is given previously here. So the block we used is “test/test” which equal class Excellence_Test_Block_Test and phtml file used is test/test.phtml
<block type="test/test" name="test" template="test/test.phtml" />
So we will create a phtml file in location app/design/frontend/default/default/templates/test/test.phtml
In the file, we are calling a function getContent(); this function is to be declared in the block Test.php block file. As we have seen previous, the $this variable here is actually an object of our block class, which we define as ‘type’ in our layout.xml. Just to confirm you write below code in your phtml file.
The output should be Excellence_Test_Block_Test.
<?php echo get_class($this); ?>
Now create the file Test.php at location app/code/local/Excellence/Test/Block/Test.php
Now, to call all these layouts we need to make some changes in the IndexController.php
class Excellence_Test_Block_Test extends Mage_Core_Block_Template
public function getContent()
return "Hello World";
Open the url www.your-magento.com/test/ again and it will again print ‘helloword’. But this time, it will print it using the 3columns layout instead of a white screen before.
class Excellence_Test_IndexController extends Mage_Core_Controller_Front_Action
public function indexAction()
$this->loadLayout(); //This function read all layout files and loads them in memory
$this->renderLayout(); //This function processes and displays all layout phtml and php files.
Controllers and URL’s
In this section I will explain how URL’s, controllers and actions are related.
Any url in magento is comprise of 3 parts e.g www.you-magento.com/test/index/index, www.you-magento.com/test/view/edit. Ff a url doesn’t have 3 part, you need to put in index yourself for example.
www.you-magento.com/test actually means www.you-magento.com/test/index/index
www.you-magento.com/test/view actually means www.you-magento.com/test/view/index
The 3 part of a URL basically means // and this same format in used in layout.xml files as well.
The comes from the value given in the tag in config.xml file. is the name of the controller file like if IndexContrller.php (index), if ViewController.php (view). this is the function name inside the controller file. For ex. indexAction(); viewAction(); testAction();
Here are few more examples. Let take the current module developed.
For this url to work we have to create a ViewController.php
Another URL: www.you-magento.com/test/view_index/xyz for this
class Excellence_Test_ViewController extends Mage_Core_Controller_Front_Action
public function deleteAction()
we will have to create a Folder called View as sub folder of controllers and then create a IndexController.php
class Excellence_Test_View_IndexController extends Mage_Core_Controller_Front_Action
public function xyzAction()
Create a new module with frontend name as test. And your module should have URL http://www.yourmagneto.com/index.php...r_view/history. Set 3 columns layout for this page and in the content block display the text “Its Working”.
View more threads in the same category: