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.