2. Magento Controller Dispatch and Hello World

Created Saturday 16 August 2014

http://alanstorm.com/magento_controller_hello_world

http://alanstorm.com/2009/img/magento-book/magento-mvc.png

Create Hello World Module

A directory structure:
app/code/local/MyLearningModules/Helloworld/Block
app/code/local/MyLearningModules/Helloworld/controllers
app/code/local/MyLearningModules/Helloworld/etc
app/code/local/MyLearningModules/Helloworld/Helper
app/code/local/MyLearningModules/Helloworld/Model
app/code/local/MyLearningModules/Helloworld/sql

A configuration file:
PATH: app/code/local/MyLearningModules/Helloworld/etc/config.xml
<config>
<modules>
<MyLearningModules_Helloworld>
<version>0.1.0</version>
</MyLearningModules_Helloworld>
</modules>
</config>

A file to activate the module:
PATH: app/etc/modules/MyLearningModules_Helloworld.xml
<config>
<modules>
<MyLearningModules_Helloworld>
<active>true</active>
<codePool>local</codePool>
</MyLearningModules_Helloworld>
</modules>
</config>

Ensure the module is active:
  1. Clear the Magento cache
  2. In the Admin go to
    1. System->Configuration->Advanced
  3. Expand "Disable Modules Output"
  4. Ensure that the module name shows up

Configuring Routes
In your config.xml add the following section:
<config>
...
<frontend>
<routers>
<helloworld>
<use>standard</use>
<args>
<module>MyLearningModules_Helloworld</module>
<frontName>helloworld</frontName>
</args>
</helloworld>
</routers>
</frontend>
...
</config>

Create Action Controller(s) for our routes:
PATH: app/code/local/MyLearningModules/Helloworld/controllers/IndexController.php
class MyLearningModules_Helloworld_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction() {
echo 'Hello Index!';
}
}

Clear the config cache and load the following URLs:
**http://example.com/helloworld/index/index**
**http://example.com/helloworld/index/**
**http://example.com/helloworld/**

Action controllers should be placed in a module's "controllers" folder

An Action Controller's name will
  1. Start with the <module> tag contents in config.xml
    1. MyLearningModules_Helloworld
  2. Be followed by an underscore (MyLearningModules_Helloworld_)
  3. Which will be followed by the Action Controller's name
    1. MyLearningModules_Helloworld_Index
  4. And finally, the word "Controller"
    1. MyLearningModules_Helloworld_IndexController
All Action Controllers need "Mage_Core_Controller_Front_Action" as an ancestor

Add a non-default method to Action Controller
Add the following code to IndexController.php
public function goodbyeAction() {
echo 'Goodbye World!';
}
Visit the URL to test it out:
http://example.com/helloworld/index/goodbye
Add the following mehod:
public function paramsAction() {
echo '<dl>';
foreach ($this->getRequest()->getParams() as $key => $value) {
echo '<dt><strong>Param: </strong>' . $key . '</dt>';
echo '<dl><strong>Value: </strong>' . $value . '</dl>';
}
echo '</dl>';
}
Visit the URL:
http://example.com/helloworld/index/params?foo=bar&bas=eof

To have the following URL
http://example.com/helloworld/messages/goodbye
Create a file:
app/code/local/MyLearningModules/Helloworld/controllers/MessagesController.php



Backlinks: