Hey buddies,
Welcome back to Magento Tutorial series!

Last week we have covered Lesson 3, Magento Folder Structure and Database. Today we will continue with lesson 4 “Configuration and naming in Magento”. There are 3 sections:

Now, let’s start!

1. Module configuration in config.xml

After the module list is loaded, the system will search for file config.xml in the module directory (app/code/[codepool]/[NameSpace]/[ModuleName]/etc/config.xml) to read the configuration of that module. The configurations are placed in the tags Within there can be the following tags:
– modules: tags including configurations for module such as active, depends, version.

<modules>
<[NameSpace_ModuleName]>
<active>[true|false]</active>
<codePool>[core|community|local]</codePool>
<depends>
<[NameSpace_ModuleName] />
</depends>
<version>(version_number)</version>
</[NameSpace_ModuleName]>
</modules>

– global: tags, including main configuration for models, resources, blocks, helpers, fieldsets, template, events, eav_attributes, custom_variables. The configurations in this section include basic configuration for module, configuration about folder containing class file for model, block, helper and events that is called (both frontend and admin), configurations for email template …

<global>
<models>
<[ModuleName]>
<class>[ClassName_Prefix]</class>
<resourceModel>[ModuleName]_[resource_model_type]</resourceModel>
<[ModuleName]_[resource_model_type]>
<!-- definition -->
</[ModuleName]_[resource_model_type]>
<rewrite><!-- definition --></rewrite>
</[ModuleName]>
</models>
<resources>
<[ModuleName]_setup><!-- definition --></[ModuleName]_setup>
<[ModuleName]_read><!-- definition --></[ModuleName]_read>
<[ModuleName]_write><!-- definition --></[ModuleName]_write>
</resources>
<blocks>
<[ModuleName]>
<class>[ClassName_Prefix]</class>
</[ModuleName]>
</blocks>
<helpers>
<[ModuleName]>
<class>[ClassName_Prefix]</class>
</[ModuleName]>
</helpers>
<fieldsets>
<[page_handle]>
<[field_name]> ><!-- definition --></[field_name]>
</[page_handle]>
</fieldsets>
<template>
<email>
<[email_template_name] module="[ModuleName]"
translate="[description]">
<!-- definition -->
<[/email_template_name]>
</email>
</template>
<events>
<[event_name]>
<observers><!-- observers --></observers>
</[event_name]>
</events>
<eav_attributes><!-- definition --></eav_attributes>
<[ModuleName]><!-- custom config variables --></[ModuleName]>
</global>

– admin: tags, including attributes, fieldsets, routers configuration. In this config tag, we usually notice about configuration for router of admin.

<admin>
<attributes>
<[attribute_name] />
<attributes>
<fieldsets><!-- definition --></fieldsets>
<routers>
<[ModuleName]>
<use>[standard|admin|default]</use>
<args>
<module>[NameSpace_ModuleName]</module>
<frontName>[frontname]</frontName>
</args>
</[ModuleName]>
<!-- or --><[ModuleName]>
<args>
<modules>
<[NameSpace_ModuleName]
before="[AnotherNameSpace_ModuleName]">
[New_ClassName]
</[NameSpace_ModuleName]
</modules>
</args>
</[ModuleName]>
</routers>
</admin>

– adminhtml: tag, including configurations of modules in admin area. The tags here are usually layout, translate, events. These configurations only take effect on backend in Magento.

<adminhtml>
<events>
<[event_name]>
<observers><!-- observers --></observers>
</[event_name]>
</events>
<global_search>
<products>
<class>[ModuleName]/search_catalog</class>
<acl>catalog</acl>
</products>
<customers>
<class>adminhtml/search_customer</class>
<acl>customer</acl>
</customers>
<sales>
<class>adminhtml/search_order</class>
<acl>sales</acl>
</sales>
</global_search>
<translate>
<modules>
<[NameSpace_ModuleName]>
<files>
<default>(name_of_translation_file.csv)
</default>
</files>
</[NameSpace_ModuleName]>
</modules>
</translate>
<layout>
<updates>
<[ModuleName]>
<file>[name_of_layout_file.xml]</file>
</[ModuleName]>
</updates>
</layout>
<[ModuleName]><!-- custom config variables --></[ModuleName]>
</adminhtml>

– frontend: tags, containing active configuration of module in frontend. It includes secure_url, events, routers, translate, layout tags. These configurations only take effect on frontend area.

<frontend>
<secure_url>
<[page_handle]>/relative/url</page_handle>
</secure_url>
<events>
<[event_name]>
<observers><!-- observers --></observers>
</[event_name]>
</events>
<routers>
<[ModuleName]>
<use>[standard|admin|default]</use>
<args>
<module>[NameSpace_ModuleName]</module>
<frontName>[frontname]</frontName>
</args>
</[ModuleName]>
</routers>
<translate>
<modules>
<[NameSpace_ModuleName]>
<files>
<default>
(name_of_translation_file.csv)
</default>
</files>
</[NameSpace_ModuleName]>
</modules>
</translate>
<layout>
<updates>
<[ModuleName]>
<file>(name_of_layout_file.xml)</file>
</[ModuleName]>
</updates>
</layout>
</frontend>

– default: tags, containing default configurations of module in the whole stores/websites.
– stores: tags, containing configurations for individual stores. In stores tag is <(store_code)> config tag for each store.
– websites: tags, containing configuration for individual website. In websites tags is <(website_code)> config tag for each website.

2. Naming classes and files in Magento

Controller

  • File name: [Namecontroller]Controller.php in directories

app/code/[Codepool/[Namespace]/[ModuleName]/controllers/
app/code/[Codepool]/[Namespace]/[ModuleName]/controllers/Adminhtml/

For example:

app/code/local/Magestore/Lesson04/controllers/Lesson04Controller.php
or app/code/local/Magestore/Bigdeal/controllers/BigdealController.php
or app/code/local/Magestore/Bigdeal/controllers/Adminhtml/BigdealController.php

  • Class name: [NameSpace]_[Module]_[Namecontroller]Controller

and [NameSpace]_[Module]_Adminhtml_ [Namecontroller]Controller.

For example: class Magestore_Lesson04_IndexController extends Mage_Core_Controller_Front_Action{}
or class Magestore_Lesson04_ Adminhtml_ReportController extends Mage_Adminhtml_Controller_Action{}

Helper

  • File name: [Namehelper].php in directory app/code/[Codepool]/[Namespace]/[Module]/helper/

For example:

app/code/local/Magestore/Lesson04/Helper/Data.php
app/code/local/Magestore/Lesson04/Helper/Lesson04.php

  • Class name: [NameSpace]_[Module]_Helper_[Filehelpername]

For example:

class Magestore_Lesson04_Helper_Lesson04 extends Mage_Core_Helper_Abstract{}

Model

  • File name:

– Model :[Namemodel].php in directory app/code/[Codepool]/[Namespace]/[Module]/Model/

– Mysql4 :[Namemodel].php in directory app/code/[Codepool]/[Namespace]/[Module]/Model/Mysql4

– Collection : Collection.php in directory app/code/[Codepool]/[Namespace]/[Module]/Model/Mysql4/([Modelname])

  • Class name:

– Model :[Namespace_Module]_Model_[Modelname] For example:
class Magestore_Lesson04_Model_Lesson04 extends Mage_Core_Model_Abstract{}

– Mysql4: [Namespace_Module]_Model_Mysql4_[Modelname] For example:
class Magestore_Lesson04_Model_Mysql4_Lesson04 extends Mage_Core_Model_Mysql4_Abstract{}

– Collection: [Namespace_Module]_Model_Mysql4__[Modelname]_Collection
For example:
class Magestore_Lesson04_Model_Mysql4_Lesson04_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract{}

Sql

  • File name:

– File install : named mysql4-install-[start-version].php in directory app/code/local/[NameSpace]/[Module]/sql/
For example:
app/code/local/Magestore/Lesson04/sql/mysql4-install-0.1.0.php
– File upgrade : named mysql4-upgrade-[old-version]-[new-version].php
For example:
app/code/local/Magestore/Lesson04/sql/mysql4-upgrade-0.1.0-0.1.1.php

Block

  • File name: [blockname].php in directories app/code/local/[NameSpace]/[Module]/Block/

and app/code/local/[NameSpace]/[Module]/Block/ Adminhtml/
For example:
App/code/local/Magestore/Lesson04/Block/Lesson04.php
App/code/local/Magestore/Lesson04/Block/ Adminhtml/Lesson04.php

3. Call Methods to Create Class objects by config and through class Mage

1. Popular call functions in class Mage

– Mage::getModel ($modelClass = ”,$arguments = array())
Retrieve model object.
– Mage:: getModuleDir($type,$moduleName)
Retrieve application module absolute path.
– Mage:: getResourceModel($modelClass,$ arguments = array())
Retrieve object of resource model.
– Mage:: getSingleton($modelClass = ”,array $arguments = array())
Retrieve model object singleton.
– Mage::getStoreConfig ($path,$id = null )
– Mage:: getUrl ($route = ”, $params = array())
Generate url by route and parameters.
– Mage::helper($name )
Retrieve helper object.
– Mage:: register($key,$value,$graceful = false) )
Register a new variable.
– Mage:: registry ($key )
Retrieve a value from registry by a key.

2. Call Methods to Create Class objects by config and through class Mage

– Mage::app()

/**
* Get initialized application object.
*
* @param string $code
* @param string $type
* @param string|array $options
* @return Mage_Core_Model_App
*/
public static function app($code = '', $type = 'store', $options = array() );

The Mage::app() function is used to bootstrap your Magento application (setting up configuration, autoloading etc) and is useful when wanting to access Magento models in your own custom script .
For example:
Mage::app()->getRequest->getParams(); Get the values sent from forms and params.
Mage::app()->getRequest->getPost(); Get the values sent from forms by post method.

– Mage::getModel()

/**
* Retrieve model object
*
* @link Mage_Core_Model_Config::getModelInstance
* @param string $modelClass
* @param array $arguments
* @return Mage_Core_Model_Abstract
*/

public static function getModel($modelClass = '', $arguments = array());

This function is defined in file: app/Mage.php
For example:
Mage::getModel(‘lesson04/lesson04’);
If file config.xml of module is configured like this:

<global>
<models>
<lesson04>
<class>Magestore_Lesson04_Model</class>
<resourceModel>lesson04_mysql4</resourceModel>
</lesson04>
<lesson04_mysql4>
<class>Magestore_Lesson04_Model_Mysql4</class>
<entities>
<lesson04>
<table>lesson04</table>
</lesson04>
</entities>
</lesson04_mysql4>
</models>

Function Mage::getModel(‘lesson04/lesson04’) will get config values of tag lesson04 in tag models (class Magestore_Lesson04_Model) and get config values of resourceModel of tag lesson04 (class Magestore_Lesson04_Model_Mysql4 ) table in database (lesson04).

– Mage::helper()

/**
* Retrieve helper object
*
* @param string $name the helper name
* @return Mage_Core_Helper_Abstract
*/
public static function helper($name);

This function is defined in file: app/Mage.php
Example 1: Mage::helper(‘lesson04’) ->test();

<helpers>
<lesson04>
<class>Magestore_Lesson04_Helper</class>
</lesson04>
</helpers>

This piece of code will process function test() in class defined in file config in (Magestore_Lesson04_Helper). This class is written in file app/code/local/Magestore/Lesson04/Helper/Data.php.

<?php
class Magestore_Lesson04_Helper_Data extends Mage_Core_Helper_Abstract
{
public function test(){
return 'data helper';
}
}

Example 2: Mage::helper(‘lesson04/newhelper’) ->test();
This piece of code will process the function test () in Magestore_Lesson04_Helper_Newhelper. This class is written in file app/code/local/Magestore/Lesson04/Helper/Newhelper.php.

<?php
class Magestore_Lesson04_Helper_Newhelper extends Mage_Core_Helper_Abstract
{
public function test(){
return 'string';
}
}

– Mage::getSingleton() :

/**
* Retrieve model object singleton
*
* @param string $modelClass
* @param array $arguments
* @return Mage_Core_Model_Abstract
*/
public static function getSingleton($modelClass='', array $arguments=array());

This function is defined in file : app/Mage.php
There’s two ways to instantiate a model class:

Mage::getModel('groupname/classname');
Mage::getSingleton('groupname/classname');

The first form will get you a new class instance. The second form will get you a singleton class instance. This particular Magento abstraction allows you to create a singleton out of any Magento model class, but only if you stick to Magento’s instantiation methods. That is, if you call

Mage::getSingleton('groupname/classname');

will return that singleton instance. (This is implemented with a registry pattern). However, there’s nothing stopping you from directly instantiating a new instance of the class with either

$o = Mage::getModel('groupname/classname');
$o = new Mage_Groupname_Model_Classname();

However, in many cases we can say
Mage::getSingleton() get object that already exist (probably because of autoload mode of magento)
Mage::getModel() get a new one object and it was configed in config.xml.

– Mage::getResourceModel()

/**
* Retrieve object of resource model
*
* @param string $modelClass
* @param array $arguments
* @return Object
*/
public static function getResourceModel($modelClass, $arguments = array());

This function is defined in file: app/Mage.php

ResourceModel is the layer that interacts with the Database objects and frames the queries.
Models are classes that use the resource models to execute business logic.
Normally this is used for the models which will communicate directly to the database.
The path is required as a parameter to the location of the resoure models
Resource models normally are found in
/app/code/core/Mage/{Module}/Model/Mysql4 /
Example

Mage::getResourceModel('catalog/product_collection')

This will get the model
/app/code/core/Mage/catalog/Model/Mysql4/product/collection.php
Or :

Mage::getModel('catalog/product') ->getCollection();

This is the end of Lesson 4. Hope you catch on the tutorial well. If you have any related question to these tutorials, feel free to let us know in the comments.

See you again in Lesson 5!

Previous lessons in the Magento Open Course series:

Author

Why Magestore? We believe in building a meaningful & long-term relationship with you.

Write A Comment