PDA

View Full Version : Create new module “HelloWorld” – in Magento



AndrewJohn26
05-16-2011, 07:17 AM
Create new module “HelloWorld” – in Magento how .?

mtthwsmith8
05-30-2011, 09:32 AM
Do you want to create a new page in Magento ? or Do you want to create a new module in Magento ? If yes, Then ok, just spend 10 minutes and follow below steps.

Target: Create a new module called “HelloWorld”
Step 1: Module Declaration
Create app/etc/modules/Tatva_HelloWorld.xml and write below code

1
<?xml version="1.0"?>

2
<config>

3
*********************************************<modules>

4
************************************************** ******************************<Tatva_HelloWorld>

5
************************************************** ************************************************** ********************<active>true</active>

6
************************************************** ************************************************** ********************<codePool>local</codePool>
7
************************************************** ******************************</Tatva_HelloWorld>

8
*********************************************</modules>

9
</config>


Step 2: Module Configuration

a. Create a controller class app/code/local/Tatva/HelloWorld/controllers/IndexController.php

1
class Tatva_HelloWorld_IndexController extends Mage_Core_Controller_Front_Action
2
{

3
********************public function indexAction()

4
********************{

5
*************************$this->loadLayout(array('default'));

6
*************************$this->renderLayout();

7
********************}

8
}


b. Create a Block class app/code/local/Tatva/HelloWorld/Block/HelloWorld.php

1
class Tatva_HelloWorld_Block_HelloWorld extends Mage_Core_Block_Template
2
{

3
**********// necessary methods

4
}


c. create configuration xml in app/code/local/Tatva/HelloWorld/etc/config.xml

01
<?xml version="1.0"?>

02
<config>

03
********************<global>

04
****************************************<modules>

05
************************************************** ******************************<tatva_helloworld>

06
************************************************** ************************************************** ********************<version>0.1.0</version>

07
************************************************** ******************************</tatva_helloworld>

08
****************************************</modules>

09
********************<blocks>

10
************************************************** **********<helloworld>

11
************************************************** ******************************<rewrite>

12
*********************************************<helloworld>Tatva_HelloWorld_Block_HelloWorld</helloworld>
13
****************************************</rewrite>

14
************************************************** **********</helloworld>

15
*************************</blocks>

16
*****

17
****************************************</global>

18
***********************************<frontend>

19
************************************************** ******************************<routers>

20
************************************************** ************************************************** ********************<helloworld>

21
************************************************** ************************************************** ************************************************** **********<use>standard</use>

22
************************************************** ************************************************** ************************************************** **********<args>

23
************************************************** ************************************************** ************************************************** ****************************************<module>Tatva_HelloWorld</module>
24
************************************************** ************************************************** ************************************************** ****************************************<frontName>helloworld</frontName>
25
************************************************** ************************************************** ************************************************** **********</args>

26
************************************************** ************************************************** ********************</helloworld>

27
************************************************** ******************************</routers>

28
****************************************<layout>

29
************************************************** **********<updates>

30
************************************************** ******************************<helloworld>

31
************************************************** ************************************************** **********<file>helloworld.xml</file>

32
************************************************** ******************************</helloworld>

33
************************************************** **********</updates>

34
************************************************** **********</layout>

35
****************************************</frontend>

36
</config>


Define Frontend Template :

1. Define page layout in app/design/frontend/Tatva/default/layout/helloworld.xml
N.B: Use default instead of Tatva as template location if you use default design packages. Means create file in app/design/frontend/default/default/layout/helloworld.xml

01
<?xml version="1.0"?>

02
*****

03
********************<layout version="0.1.0">

04
*****

05
****************************************<helloworld_index_index>

06
************************************************** **********<reference name="root">

07
************************************************** ******************************<action method="setTemplate"><template>page/1column.phtml</template></action>
08
************************************************** **********</reference>

09
************************************************** **********<reference name="content">

10
************************************************** ******************************<block type="helloworld/helloworld" name="hello" template="helloworld/helloworld.phtml"/>
11
************************************************** **********</reference>

12
****************************************</helloworld_index_index>

13
*****

14
********************</layout>


2. Create template file app/design/frontend/Tatva/default/template/helloworld/helloworld.phtml and write down
N.B: Use default instead of Tatva as template location if you use default design packages. Means create file in app/design/frontend/default/default/template/helloworld/helloworld.phtml
Hello World ! I am a Magento Guy..
Hey, new module is ready to run and hit browser with url

http://127.0.0.1/projectname/index.php/helloworld/

Kate Scott
06-02-2011, 08:19 AM
After reading through everything I can find I’ve nearly been able to create a simple custom module as per the tutorials/threads etc, the one thing that isn’t working is the layout xml for the module, it seems to be completely ignored - I think I’m missing something so basic no one every posts it, every thread related to this topic seems to end with “oh I’ve got it working now” and no final explanation wink
Hopefully someone can help clear this up for me!
To strip it back to complete basics I’m trying to create a Hello World block in a custom module which is displayed in whatever the current theme is.

Here’s what I’ve done that nearly works:

Created a new class in /app/code/local/Mycompany/Hello/Block/View.php

<?php
class Mycompany_Hello_Block_View extends Mage_Core_Block_Template {
private $message;

protected function createMessage($msg) {
$this->message = $msg;
}

public function receiveMessage() {
if($this->message != '') {
return $this->message;
} else {
$this->createMessage('Hello World');
return $this->message;
}
}
}


<?xml version="1.0"?>
<config>
<modules>
<Mycompany_Hello>
<version>0.1.0</version>
</Mycompany_Hello>
</modules>
<global>
<blocks>
<hello>
<class>Mycompany_Hello_Block</class>
</hello>
</blocks>
</global>
</config>


<config>
<modules>
<Mycompany_Hello>
<active>true</active>
<codePool>local</codePool>
<version>0.1.0</version>
</Mycompany_Hello>
</modules>
</config>

There is a phtml template for this as well, in /frontend/default/default/Mycompany/hello.phtml

<div class="block-content">
<h1>HERE I AM</h1>
<?php
Zend_Debug::dump($this);
echo $this->receiveMessage();?>
</div>

Now I want to show the results on the product info page in the current theme. So if I edit /frontend/default/default/layout/catalog.xml I can call this module and template and it works fine

<block type="catalog/product_view" name="product.info" template="catalog/product/view.phtml">
...
<block type="hello/view" name="Mycompany.hello.block" as="hello_block" template="Mycompany/hello.phtml" />
...
</block>

And to show it I call the block inside the product/view.phtml file

<?php echo $this->getChildHtml('hello_block');?>

This works but here’s where I get lost - what I don’t understand is why I can’t seem to create a new layout xml file for this module, I can’t seem to get the syntax right.

From all the other threads this seems to be the ‘right thing to do’ and I thought I should be able to avoid modifying the default/layout/catalog.xml and instead create a default/layout/hello.xml file like this:
<layout version="0.1.0">
<hello_index_index>
<reference name="product.info">
<block type="hello/view" name="Mycompany.hello.block" as="hello_block" template="Mycompany/hello.phtml" />
</reference>
</hello_index_index>
</layout>

I’ve tried <hello>, <hello_index>, <hello_index_index> and even breaking the xml <he llo> to force an error, but the file doesn’t appear to be read at all. Have I just got the filename wrong or am I barking up the wrong tree?

I’m sure this is a really, really, simple mistake but I’m stumped!

Based on your layout handle <hello_index_index> You are missing the controller:

\app\code\local\Mycompany\Hello\controllers\IndexC ontroller.php:

class Mycompany_Hello_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout();
$this->renderLayout();
}
}
The URL is

Mage::getBaseUrl('hello/index'); //yourdomain.com/hello/index/

shoponics
05-19-2014, 04:14 AM
thanx for sharing nice information with us i strongly believe it is helpful for me and my other forum friends who want to more information about this topic.

alexmorco
09-26-2018, 07:30 AM
Customizing module with Magento layout XML (https://www.cloudways.com/blog/customize-magento-modules-through-layout-xml-files/) file is very easy, you can add custom titles, meta tags, keywords or a description of your Magento module page. You can easily add this custom information to all existing, core or newly created modules.