Magento 1.9 Frontend Controllers – Adding New Page to Front End

catalogsearchcontrollersfrontendmagento-1.9product-view

UPDATED

I am trying to add new page on front end. I am doing as following. I have a form, where I am using my action to load the view. On button click it never goes to controller, please help me, what I am doing wrong.

My form is as

<div id="abc-category-search">
    <form id="abc-category-search-form" method="post" action="<?php echo Mage::getUrl('helloworld') ?>">
        <label for="search"><?php echo $this->__('Search by product SKU/Category') ?>:</label>
        <input id="abc-category-search-string" name="search" value="" />
        <input id="abc-category-search-cat" type="hidden" name="category" value="<?php echo $category; ?>" />
        <button id="abc-category-search-submit" type="submit" class="engine-btn" value="" >
            <?php echo $this->__('Search') ?>
        </button>
    </form>
</div>

And I am adding page like this.

app/code/local/Abc/Helloworld/etc/config.xml

        <?xml version="1.0"?>
<config>
    <global>
        <modules>
            <Abc_Helloworld>
                <version>
                    0.1.0
                </version>
            </Abc_Helloworld>
        </modules>
        <blocks>
            <abc_helloworld>
                <class>Abc_Helloworld_Block</class>
            </abc_helloworld>
        </blocks>
    </global>
    <frontend>
        <routers>
            <helloworld>
                <use>standard</use>
                <args>
                    <module>Abc_Helloworld</module>
                    <frontName>helloworld</frontName>
                </args>
            </helloworld>
        </routers>
        <layout>
            <updates>
                <helloworld>
                    <file>abc_helloworld.xml</file>
                </helloworld>
            </updates>
        </layout>

    </frontend>
</config>

And

app/code/local/Abc/Helloworld/controllers/IndexController.php

<?php

class Abc_Helloworld_IndexController extends Mage_Core_Controller_Front_Action {
    public function indexAction() {
        $this->loadLayout();
        $this->renderLayout();
    }
}

And

    app/code/local/Abc/Helloworld/Block/Helloworld.php
<?php

    class Abc_HelloWorld_Block_Helloworld extends Mage_Core_Block_Template {
        // necessary methods
    }

And Layout file is

 app/design/frontend/default/Jin/layout/abc_helloworld.xml


<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">
  <helloworld_index_index>
    <reference name="content">
      <block type="abc_helloworld/helloWorld" name="helloworld_any_block" template="helloworld/index.phtml" />
    </reference>
  </helloworld_index_index>
</layout>

And .phtml

app/design/frontend/default/jin/template/helloworld/helloworld.phtml

<div>
    <?php
        echo 'Abdul Ghaffar'; 
    ?>
</div>

Best Answer

Problem resides in your layout update xml file, particularly in the following block declaration.

<block type="abc_helloworld/index" name="helloworld_any_block" template="helloworld/index.phtml" />

Magento fails to find this block, because you don't have block declaration in your config.xml file. So you need to update your config.xml file with following code:

<config>
    <global>
        <blocks>
            <abc_helloworld>
                <class>Abc_HelloWorld_Block</class>
            </abc_helloworld>
        </blocks>
    </global>
</config>

Here you are declaring your block group name. That's it.

If you want to use your own custom block, then the reference to your block will also need to change like this.

<block type="abc_helloworld/helloWorld" name="helloworld_any_block" template="helloworld/index.phtml" />

Difference here is, I am using abc_helloworld/helloWorld instead of abc_helloworld/index. This is because your block type reference looks for a block Abc_HelloWorld_Block_Index and which is not defined.

Also make sure default/jin is your current package theme. Otherwise it would be better to move your phtml template file to base/default package theme directory since it is the final fallback location for Magento.

Make sure you clear all cache after all these changes made. Happy coding

EDIT - 1

Your form action is also need to be changed.

It should be like

<form id="abc-category-search-form" method="post" action="<?php echo Mage::getUrl('helloworld') ?>">

Let Magento generate correct URL for you. :)

EDIT - 2

Use below config.xml file

<?xml version="1.0"?>
    <config>
        <modules>
            <Abc_HelloWorld>
                <version>0.1.0</version>
            </Abc_HelloWorld>
        </modules>
        <global>
            <blocks>
                <abc_helloworld>
                    <class>Abc_HelloWorld_Block</class>
                </abc_helloworld>
            </blocks>
        </global>
        <frontend>
            <routers>
                <helloworld>
                    <use>standard</use>
                    <args>
                        <module>Abc_HelloWorld</module>
                        <frontName>helloworld</frontName>
                    </args>
                </helloworld>
            </routers>
            <layout>
                <updates>
                    <helloworld>
                        <file>abc_helloworld.xml</file>
                    </helloworld>
                </updates>
            </layout>
        </frontend>
    </config>

Main difference here is, you use Helloworld instead of HelloWorld in almost all configuration and I also changed router frontname to helloworld instead of Helloworld.

Make sure you have activation file for you module

File : app\etc\modules\Abc_HelloWorld.xml

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

FINAL EDIT

Magento is Letter sensitive. Both me and digitalPianism says samething. You are using the file structure Abc\HelloWorld. Hence you need to use same naming concept. ie Abc_HelloWorld. Or if you like to have a class name Abc_Helloworld, then your file structure should be Abc/Helloworld.

Now about layout update xml file. You have a template file helloworld/helloworld.phtml and hence you need to specify it correctly in your block. ie

<block type="abc_helloworld/helloWorld" name="helloworld_any_block" template="helloworld/helloworld.phtml" />

This block will generally say 3 things to Magento.

Assumes your module is Abc_HelloWorld and not Abc_Helloworld and your file structure is like Abc/HelloWorld

  1. My block is of type = abc_helloworld/helloWorld. This will be read by Magento like Abc_HelloWorld_Block_HelloWorld.

  2. My block name is helloworld_any_block

  3. Use app\design\frontend\{package}\{theme}\template\helloworld/helloworld.phtml as my block's template.
Related Topic