Tuesday , December 12 2017
Home >> All tutorial >> Magento Tutorials: Module Initialization (Part 5)

Magento Tutorials: Module Initialization (Part 5)

In order to keep you following our Magento Tutorials, today, we will tell you about Magento Module Initialization. This section has 4 contents as below:

  • Describe/identify the steps needed to create and register a new module
  • Describe/identify module dependencies
  • Describe/identify the steps needed to create a controller in a module
  • Describe/identify the steps needed to enable and disable a module

1. Describe/identify the steps needed to create and register a new module
Creating a custom module is indispensible to customize Magento. Modules may be from simple forms such as Static Block or quite complicated forms like payment/shipping module and more complex modules (for example: integrating with the 3rd modules).
A custom can do a lot such as impacting database in overriding classes (Blocks, Controllers and Modules)… and more.
How to create a module and use it in a CMS page? We will guide you in this part with simple steps.
We will create Magestore Example module with Magestore is NameSpace and Example is name of the module.
Step1: Register the module with the system
Create a file app/etc/modules/Magestore_Example.xml with content:

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

The primary parameters of this part are:

  • Module name: Name definition of the module
  • Active: Status definition of the module
  • CodePool: Definition of the folder that stores code of the module. The values are usually: core, local and community.

Step2: Create file config.xml of the module
Generate file file app/code/local/Magestore/Example/etc/config.xml.

<?xml version="1.0> <config> <modules> <Magestore_Example> <version>0.1.0</version> </Magestore_Example> </modules> <global> <blocks> <Magestore_Example> <class>>Magestore_Example_Block<</class> </Magestore_Example> </blocks> </global> </config> 

In this part, we declare the version of the module and block class prefix Magestore_Example_Block
Step 3: Create Block file
Generate file: app/code/local/Magestore/Example/Block/View.php

<?php class Magestore_Example_Block_View extends Mage_Core_Block_Template { public function sayHello() { return “Hello World!”; } } ?> 

Step 4: Create file template (phtml)
Generate file app\design\frontend\default\default\template\example\view.phtml.

<div> <span><strong>This is the output of Magestore example:</strong></span></br> <?php echo $this-> sayHello(); ?> </div> 

We will have a fully-made module after these 4 steps. Now we will test the module action by putting it into a CMS page.

{{block type="magestore_example/view" template="example/view.phtml" }} 

2. Describe/identify module dependencies
Module dependencies is a definition which illustrates the action dependencies of a module on another module. A simple example is as below:

<?xml version="1.0> <config> <modules> <Magestore_Bundle> <active>true</active> <codePool>core</codePool> <depends> <Mage_Catalog/> <depends> <Magestore_Bundle> <modules> </config> 

If the module Mage_Bundle is active and Mage_Catalog does not exist or be inactive, the system will report the issue and stop the execution.
Besides, the module dependency is inserted in code when the system checks the exection or in configuration, for example: configure in the following file adminhtml.xml (Menu CMS in admin will be hidden if the module Magestore_Student does not exist or be inactive).

<?xml version="1.0> <config> <menu> <cms> <depends> <module>Magestore_Test</module> <depends> <Mage_Catalog/> <depends> <cms> <menu> </config> 

3. Describe/identify the steps needed to create a controller in a module
In the first part, we can know how to create a simple module. Now, we keep using the last result and add steps below:
Step 1: Create IndexController
In this step, we create a controller with name Index.
Create file: app/code/local/Magestore/Example/controllers/IndexController.php
Controller will be generated according to a sample with the file name is: xxxxxController.php (this file is put in the category app/code/local/Magestore/Example/controllers).
Each action in the controller is declared based on the sample: yyyyyAction (yyyyyAction is a function in the controller).

class Magestore_Example_IndexController extends Mage_Core_Controller_Front_Action { public function sayhelloAction(){ } }

Step 2: Edit Config.xml file
Edit file: app/local/Magestore/Example/etc/config.xml

 <config> ... <frontend> <routers> <example> <use>standard</use> <args> <module>Magestore_Example</module> <frontName>example</frontName> </args> </example> </routers> </frontend> </config> 

Some configuration tags used:

  • <frontend>: Show that this router will be used in frontend of website.
  • <routers>: The place to define routers.
  • <example>: The ID value of routers.
  • <use>standard</use> Can receive values of standard (for frontend area) or admin (for admin area).
  • <module>Magestore_Example</module>: Show which module will use this router.
  • <frontName>example</frontName>: Name of the router which is used on URL

Step 3: Display a template
We need to declare a layout file which is loaded by adding the code below to config.xml (the configuration file of the module):

 <config> ... <frontend> ... <layout> <updates> <example> <file>example.xml</file> </example> </updates> </layout> </frontend> </config> 

And then, create the file app/design/frontend/default/default/layout/example.xml with the following content:

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

The major template is used by example/index/sayhello is page/1column.phtml and the template content is displayed on block example_index_view. This block uses the file template example/view.phtml.
There is still a small detail for us to complete the process. We need to edit the function sayhello in controller Index.

public function sayhelloAction(){ $this->loadLayout(); $this->renderLayout(); }

You can view your successful result by requesting to URL (in browser): www.localhost.com/your_website/example/index/sayhello
4. Describe/identify the steps needed to enable and disable a module
At first, plese take a look at app/etc/modules/Magestore_Example.xml in the first part.

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

We have 2 ways to disable a module:

  • Change from <active>true</active> to <active>false</active>
  • Delete this file Magetore_Example.xml.

Now you can easily create a new module and take necessary actions in Magento for the Initialization? Hope this article brings you great helps! ;)

We also contribute great content of Magento Download for FREE (all versions)

DON'T MISS THE CHANCE!

magento download

About Champ

2 comments

  1. karen millen dresses

    Thank you incredibly substantially for your exciting text. I have been looking for these types of message to get a definitely very long time. Thank you.

  2. We’re a group of volunteers and opening a new scheme in our community. Your site provided us with valuable info to work on. You have done a formidable job and our whole community will be grateful to you.

Leave a Reply

Your email address will not be published. Required fields are marked *

Heap | Mobile and Web Analytics