Writing a Magento Extension - part2

Posted on August 9, 2011 by admin There have been 2 comment(s)

On this part, we will make the text "Hello World" appear inside the Magento design.

For this, go to app/code/local/Engineering/Helloworld/controllers/IndexController.php and change its code to following:

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

A short explanation:
When calling to

$this->loadLayout();

Magento will go to "app/design/myinterface/mytheme/layout" (on a fresh Magento installation, it actually "app/design/base/default/layout") reads all the XML files and searches there for a tag named layout and loads the found data to "core/layout" object.
Now, when calling to

$this->renderLayout();

the layout will be rendered.

Now lets go to www.mysite.com/helloworld - we will see some empty standard Magento design. It's because only tags get loaded.

Now go to app/code/local/Engineering/Helloworld/etc/config.xml and add some change to it:

<?xml version="1.0"?>
<config>
 <modules>
  <Engineering_Helloworld>
   <version>0.1.0</version>
  </Engineering_Helloworld>
 </modules>
 <frontend>
  <routers>
   <engineering_helloworld>
    <use>standard</use>
    <args>
     <module>Engineering_Helloworld</module>
     <frontName>helloworld</frontName>
    </args>
   </engineering_helloworld>
  </routers>
  <layout>
   <updates>
    <engineering_helloworld>
     <file>helloworld.xml</file>
    </engineering_helloworld>
  </updates>
  </layout>
 </frontend>
</config>

This tells magento to regard the file named helloworld.xml as another layout file. Now create the helloworld.xml at:
app/design/frontend/default/default/layout/helloworld.xml
Type in the following:

<?xml version="1.0"?>
<layout version="0.1.0">
 <engineering_helloworld_index_index>
  <reference name="content">
   <block type="core/template" name="helloworld.text"
   template="engineering/helloworld/sayhello.phtml"/>
  </reference>
 </engineering_helloworld_index_index>
</layout>

Because the URL is www.mysite.com/helloworld/index/index magento will load the content of <helloworld_index_index> tag to the layout object.  Afterwards, the

$this->renderLayout();

will put the block named "helloworld.text" to the block named "content",  load the block class "core/template" (Mage_Core_Block_Template) and template "engineering/helloworld/sayhello.phtml" (app/design/frontend/myinterface/mytheme/engineering/helloworld/sayhello.phtml)  for this block.

Now, create "sayhello.phtml" at
app/design/frontend/default/default/template/engineering/helloworld/sayhello.phtml
type there just

Hello World

Now, go to www.mysite.com/admin/helloworld - you should see the text "Hello world" inside magento design.


This post was posted in Writing a Magento Extension and was tagged with magento extension, hello world, magento module

2 Responses to Writing a Magento Extension - part2

  • ron
    ron says:

    i followed the instructions from part1 & part2 and my extension is not working!!! what do I do wrong???

    Posted on September 11, 2012 at 08:04

  • ron
    ron says:

    forget about it - thanks! turns out
    <layout version="0.1.0">
    <engineering_helloworld_index_index>

    is case sensitive and didnt work when I changed to Env_Ron_index_index

    fixed

    Posted on September 11, 2012 at 08:24

Comments
Magento is a well-engineered eCommerce platform designed to help engineers develop customized eCommerce online stores. Due to lack of proper coding documentation, Engineer-ing.com was created with the sole purpose of instructing Magento developers to-be with the "how-to-do" know-how. In the event of unresolved issues, you are more than welcome to contact me for consultation. However, please do so only if you possess a Software Engineering background and you're able to specify your question.