Lesson 5.1: Create Magento Modules

Howdy friends! Welcome back to Magestore’s Magento Tutorial series.

Last week we have learned about Configuration and naming in Magento. Lesson 5 will cover an interesting topic: How to create and Upgrade Magento Modules.

Lesson 5.1 Create a Magento Module

Lesson 5.2 Upgrade Magento Modules

As you know, Magento is a built on top of the Zend Framework. The Zend Framework supports modularizing your application, and Magento went the way of the module as part of its core design. In this short Magento Open Course lesson, I will explain how to use the Module Creator to help you create modules quickly.

Creating new Magento Modules by using Module Creator

The Module Creator allows you to fill in a few questions (Module Name, Namespace, Magento Root Dir, and some Design questions), and then will auto-generate all the files necessary to get you started on your module. The coolest thing about the Module Creator is the way you can create templates/skeletons/frameworks of modules and store them for later use. Module Creator is generally a time saver and recommended for a noob.

Step 1:

Using a browser, download the latest full released archive file from https://github.com/shell/Magento-Module-Creator
On your computer, extract Module Creator from the downloaded archive. For Module Creator has a folder named ModuleCreator containing all of the Module Creator files will be created

Step 2:

Copy magento folder after extract Module Creator from the downloaded archive to xampp\htdocs

Step 3:

Using a browser, enter the url: to start creating new Magento Modules.
Please fill information for Namespace and Module(Module name)
Eg: Namespace: Magestore
Module: lesson05

Step 4:

Click “Create” button, the all source codes of new Magento Modules will be generated in xampp\htdocs\ModuleCreator\new
After create new Magento Modules Studentmanager by using Module Creator, we can see:

  • Declare Magestore_Lesson05 module in file app/etc/modules/Magestore_Lesson05.xml
<?xml version="1.0"?>
<!--
/**
* Magestore
*
* Online Magento Course
*
*/
-->
<config>
<modules>
<!-- declare Magestore_Lesson05 module -->
<Magestore_Lesson05>
<active>true</active>
<codePool>local</codePool>
</Magestore_Lesson05>
</modules>
</config>
  • Declare module’s version information in file app/code/local/Magestore/Lesson05/etc/config.xml
<?xml version="1.0"?>
<!--
/**
* Magestore
*
* Online Magento Course
*
*/
-->
<config>
<modules>
<!-- declare module's version information -->
<Magestore_Lesson05>
<!-- this version number will be used for database upgrades -->
<version>0.1.0</version>
</Magestore_Lesson05>
</modules>
<global>
<!-- declare model group for new module -->
<models>
<!-- model group alias to be used in Mage::getModel() -->
<lesson05>
<!-- base class name for the model group -->
<class>Magestore_Lesson05_Model</class>

<!-- declare model lesson05 work with lesson05 table -->
<entities>
<lesson05>
<table>lesson05</table>
</lesson05>
</entities>
</lesson05>
</models>

<!-- declare resource setup for new module -->
<resources>
<!-- resource identifier -->
<lesson05_setup>
<!-- specify that this resource is a setup resource and used for upgrades -->
<setup>
<!-- which module to look for install/upgrade files in -->
<module>Magestore_Lesson05</module>
</setup>
<!-- specify database connection for this resource -->
<connection>
<!-- do not create new connection, use predefined core setup connection -->
<use>core_setup</use>
</connection>
</lesson05_setup>
</resources>
</global>
</config>
  • Lesson05 modelapp/code/local/Magestore/Lesson05/Model/Lesson05.php
<?php
/**
* Magestore
*
* Online Magento Course
*
*/

/**
* Lesson05 Model
*
* @category Magestore
* @package Magestore_Lesson05
* @author Magestore Developer
*/
class Magestore_Lesson05_Model_Lesson05 extends Mage_Core_Model_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('lesson05/lesson05');
}
}
  • Lesson05 resource model:app/code/local/Magestore/Lesson05/Model/Mysql4/Lesson05.php
<?php
/**
* Magestore
*
* Online Magento Course
*
*/

/**
* Lesson05 Resource Model
*
* @category Magestore
* @package Magestore_Lesson05
* @author Magestore Developer
*/
class Magestore_Lesson05_Model_Mysql4_Lesson05 extends Mage_Core_Model_Mysql4_Abstract
{
public function _construct()
{
/* lesson05_id is primary of lesson05 table */
$this->_init('lesson05/lesson05', 'lesson05_id');
}
}
  • Lesson05 Resource Collection Model:app/codel/local/Magestore/Lesson05/Model/Mysql4/Lesson05/Collection.php
<?php
/**
* Magestore
*
* Online Magento Course
*
*/

/**
* Lesson05 Resource Collection Model
*
* @category Magestore
* @package Magestore_Lesson05
* @author Magestore Developer
*/
class Magestore_Lesson05_Model_Mysql4_Lesson05_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('lesson05/lesson05');
}
}
  • All file sql to install database of module inapp/code/local/Magestore/Lesson05/sql/lesson05_setup

To this moment, I guess you have all know how to create new Magento modules. In the next article, we will go to part 2: Creating our Installer Script and learn to Upgrade Magento modules. For more Magento lesson, you can search on our blog with the tag Magento Open Course.

Follow up our blog! See you soon!

Previous lessons in Magento Open Course series: