2
votes

Hello, guys!

I really hope someone will help me with this.
I trying to make new module in magento 1.9.3.2, which show "Hello world!" from phtml file. I created that step by step by this rules. Module is created, but when I open module (127.0.0.1/magento/helloworld) in browser, nothing appear, just empty this template. opened module in browser - screenshot

Here is steps by what i guided:
1. Module declaration: Create new xml file in app/etc/modules/M4U_HelloWorld.xml
<?xml version="1.0"?> <config> <modules> <M4U_HelloWorld> <active>true</active> <codePool>local</codePool> </M4U_HelloWorld> </modules> </config>

  1. Module configuration
    2.1. Create controller class in app/code/local/M4U/HelloWorld/controllers/IndexController.php

    class M4U_HelloWorld_IndexController extends Mage_Core_Controller_Front_Action { public function indexAction() { $this->loadLayout(array('default')); $this->renderLayout(); } }

2.2. Create Block class in app/code/local/M4U/HelloWorld/Block/HelloWorld.php

class M4U_HelloWorld_Block_HelloWorld extends Mage_Core_Block_Template
                  {
                       // necessary methods
                  }

2.3. Create configuration xml in app/code/local/M4U/HelloWorld/etc/config.xml

<?xml version="1.0"?>
<config>
<global>
    <modules>
            <m4u_helloworld>
                    <version>0.1.0</version>
            </m4u_helloworld>
    </modules>
<blocks>
        <helloworld>
            <rewrite>
     <helloworld>M4U_HelloWorld_Block_HelloWorld</helloworld>
    </rewrite>
        </helloworld>
 </blocks>
    </global>
   <frontend>
            <routers>
                    <helloworld>
                            <use>standard</use>
                            <args>
                                  <module>M4U_HelloWorld</module>
                                  <frontName>helloworld</frontName>
                            </args>
                    </helloworld>
            </routers>
    <layout>
        <updates>
            <helloworld>
                  <file>helloworld.xml</file>
            </helloworld>
        </updates>
        </layout>
    </frontend>

  1. Define frontend template
    3.1. Define page layout in app/design/frontend/default/default/layout/helloworld.xml
    <?xml version="1.0"?> <layout version="0.1.0"> <helloworld_index_index> <reference name="root"> <action method="setTemplate"> <template>page/1column.phtml</template></action> </reference> <reference name="content"> <block type="helloworld/helloworld" name="hello" template="helloworld/helloworld.phtml"/> </reference> </helloworld_index_index> </layout>

  2. Create template file in app/design/frontend/default/default/template/helloworld/helloworld.phtml

3
instead of 127.0.0.1/magento/arturs try 127.0.0.1/magento/helloworld since that's what you've defined the URL as in the <frontent><routers> in config.xmlCD001
Oi, sorry, i forget change in this post, it was a helloworldArturs
In config.xml you've got <modules> inside <global> - it should be root level.CD001
Possible duplicate of How to create new page in magento siteLuFFy

3 Answers

4
votes

TRIED AND TESTED

screenshot here

you can display your helloworld template in your custom module, done some minor modifications to your code.

  1. module declaration:

    <?xml version="1.0"?>
     <config>
           <modules>
              <M4U_HelloWorld>
                   <active>true</active>
                   <codePool>local</codePool>
              </M4U_HelloWorld>
           </modules>
     </config>
    
  2. creating the folder structure and add the files

A. app/code/local/M4U/HelloWorld/etc/config.xml

<config>
    <modules>
        <m4u_helloworld>
            <version>0.1.0</version>
        </m4u_helloworld>
    </modules>
    <frontend>
        <routers>
            <helloworld>
                <use>standard</use>
                <args>
                    <module>M4U_HelloWorld</module>
                    <frontName>helloworld</frontName>
                </args>
            </helloworld>
        </routers>
        <layout>
            <updates>
                <helloworld module="M4U_HelloWorld">
                    <file>M4U_HelloWorld.xml</file>
                </helloworld>
            </updates>
        </layout>
    </frontend>
    <global>
        <blocks>
            <helloworld>
                <class>M4U_HelloWorld_Block</class>
            </helloworld>
        </blocks>
    </global>
</config>

B. app/code/local/M4U/HelloWorld/block/HelloWorld.php

class M4U_HelloWorld_Block_HelloWorld extends Mage_Core_Block_Template {

}

C. app/code/local/M4U/HelloWorld/controllers/IndexController.php

class M4U_HelloWorld_IndexController extends Mage_Core_Controller_Front_Action {

    public function indexAction() {
        echo 'hello world';
        $this->loadLayout();  //This function read all layout files and loads them in memory
        $this->renderLayout();
    }

}

D. app/design/frontend/ * theme base* / * mytheme * /layout/M4U_HelloWorld.xml

<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">
    <helloworld_index_index>
        <reference name="root">
            <action method="setTemplate">                  
                <template>page/1column.phtml</template>
            </action>
        </reference>
        <reference name="content">
            <block type="helloworld/helloworld" name="hello" template="helloworld/helloworld.phtml"/>
        </reference>
    </helloworld_index_index>
</layout>

E. app/design/frontend/ * theme base* / * mytheme * /template/helloworld/helloworld.php

echo 'im a template block';
1
votes

For create HelloWorld module in Magento 1.9 please follow below tutorial URL.

URL: http://blog.iyngaran.info/create-custom-module-helloworld-in-magento

0
votes

As Per Inchoo Article :

We will start by creating a simple “Hello world” module. However, you will soon see that simple takes a new meaning with Magento. Creating a bare bone module requires at least two files in Magento. For your module to work you need /app/etc/modules/MyCompany_MyModule.xml, app/code/local/MyCompany/MyModule/etc/config.xml. But, barebone module will not give you a “Hello developer” -So, we need to add few more files to the game.

file 1: /app/etc/modules/Inchoo_HelloDeveloper.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Inchoo_HelloDeveloper>
            <active>true</active>
            <codePool>local</codePool>
        </Inchoo_HelloDeveloper>
    </modules>
</config>

file 2: app/code/local/Inchoo/HelloDeveloper/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Inchoo_HelloDeveloper>
            <version>0.1.0</version>
        </Inchoo_HelloDeveloper>
    </modules>    
    <frontend>
        <routers>
            <Inchoo_HelloDeveloper_SomeFreeRouterName1>
                <use>standard</use>
                <args>
                    <module>Inchoo_HelloDeveloper</module>
                    <frontName>inchoo-hellodeveloper</frontName>
                </args>
            </Inchoo_HelloDeveloper_SomeFreeRouterName1>
        </routers>
    </frontend>    
</config>

file 3: app/code/local/Inchoo/HelloDeveloper/controllers/IndexController.php

<?php

class Inchoo_HelloDeveloper_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        echo 'Hello developer...';
    }

    public function sayHelloAction()
    {
        echo 'Hello one more time...';
    }
}
?>

Altough extremely simple, file 3 shows us one important thing: Naming connvention. Note the name of the class. Your moudle classes should keep names in form of MyCompany_MyModule_FileName, or in case of block and modules: MyCompany_MyModule_Block_FileName or MyCompany_MyModule_Module_FileName.