Home > Backend Development > PHP Tutorial > How to Create a HelloWorld Module and Router in Magento?

How to Create a HelloWorld Module and Router in Magento?

Linda Hamilton
Release: 2024-12-02 06:59:08
Original
368 people have browsed it

How to Create a HelloWorld Module and Router in Magento?

Creating the HelloWorld Module and Router

To create a simple HelloWorld module and configure its router:

  1. Create the Module XML:

    • Create an XML file named MyCompanyName_HelloWorld.xml in app/modules.
    • Include the following code in the file:
    <config>
        <modules>
            <MyCompanyName_HelloWorld>
                <active>true</active>
                <codePool>local</codePool>
            </MyCompanyName_HelloWorld>
        </modules>
    </config>
    Copy after login
  2. Configure the Router:

    • In the etc/config.xml file of your module, add the following section:
    <config>
        <frontend>
            <routers>
                <helloworld>
                    <use>standard</use>
                    <args>
                        <module>MyCompanyName_HelloWorld</module>
                        <frontName>helloworld</frontName>
                    </args>
                </helloworld>
            </routers>
        </frontend>
    </config>
    Copy after login
  3. Create the FrontName Controller:

    • In app/code/local/MyCompanyName/HelloWorld/controllers, create a file called IndexController.php.
    • Include the following code in the file:
    class MyCompanyName_HelloWorld_IndexController extends Mage_Core_Controller_Front_Action {
        public function indexAction() {
            echo "Hello World";
        }
    }
    Copy after login
  4. Refresh Cache:

    • Navigate to System > Cache Management and refresh the application cache.

Adding a Controller and Model Interaction

To add a controller that interacts with a model:

  1. Create the Controller:

    • Create a new file named ShowRowController.php in app/code/local/MyCompanyName/HelloWorld/controllers.
    • Include the following code in the file:
    class MyCompanyName_HelloWorld_ShowRowController extends Mage_Core_Controller_Front_Action {
        public function indexAction() {
            $row = Mage::getModel('mymodel/mymodel')->load(10);
            echo $row->getData('id');
        }
    }
    Copy after login
  2. Configure the Router (if necessary):

    • Add the following router configuration to etc/config.xml:
    <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>
    Copy after login
  3. Create the Model:

    • Create a new file named MyModel.php in app/code/local/MyCompanyName/HelloWorld/Model.
    • Include the following code in the file:
    class MyCompanyName_HelloWorld_Model_MyModel extends Mage_Core_Model_Abstract {
        protected function _construct() {
            $this->_init('mymodel/mymodel');
        }
    }
    Copy after login
  4. Refresh Cache:

    • Refresh the application cache as before.

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) {
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template