Creating the HelloWorld Module and Router
To create a simple HelloWorld module and configure its router:
Create the Module XML:
<config> <modules> <MyCompanyName_HelloWorld> <active>true</active> <codePool>local</codePool> </MyCompanyName_HelloWorld> </modules> </config>
Configure the Router:
<config> <frontend> <routers> <helloworld> <use>standard</use> <args> <module>MyCompanyName_HelloWorld</module> <frontName>helloworld</frontName> </args> </helloworld> </routers> </frontend> </config>
Create the FrontName Controller:
class MyCompanyName_HelloWorld_IndexController extends Mage_Core_Controller_Front_Action { public function indexAction() { echo "Hello World"; } }
Refresh Cache:
Adding a Controller and Model Interaction
To add a controller that interacts with a model:
Create the Controller:
class MyCompanyName_HelloWorld_ShowRowController extends Mage_Core_Controller_Front_Action { public function indexAction() { $row = Mage::getModel('mymodel/mymodel')->load(10); echo $row->getData('id'); } }
Configure the Router (if necessary):
<config> <frontend> <routers> <helloworld> <use>standard</use> <args> <module>MyCompanyName_HelloWorld</module> <frontName>helloworld</frontName> </args> </helloworld> <show_row> <use>standard</use> <args> <module>MyCompanyName_HelloWorld</module> <frontName>show_row</frontName> </args> </show_row> </routers> </frontend> </config>
Create the Model:
class MyCompanyName_HelloWorld_Model_MyModel extends Mage_Core_Model_Abstract { protected function _construct() { $this->_init('mymodel/mymodel'); } }
Refresh Cache:
Using SQL Queries
While it's generally not recommended to use raw SQL queries in Magento, you can access model objects to retrieve data. For example:
$articles = Mage::getModel('articles/articles')->getCollection(); foreach ($articles as $article) { if ($article->getId() == 10) {
The above is the detailed content of How to Create a HelloWorld Module and Router in Magento?. For more information, please follow other related articles on the PHP Chinese website!