Magento For Developers: Magento Config

Do you know Magento? You may already know Magento to be the most powerful ecommerce platform today. What else’s thing you may not realize that is Magento’s object – oriented PHP Framework which can be used to develop web applications tapping into powerful features of Magento.

The beating heart of Magento System is the config that describes any module, model, template you need to access. Most PHP developers are not working with an abstraction level, but it allows you an amount of flexibility as far as override default system.

1. Set Up a Module Directory Structure

Now let’s start creating a module. A Magento module is grouped by php and xml files extending the system with new functionality or overriding core system behavior. This may also means to add additional data models which are transforming current classes’ behavior or create new features. Notice that the basic Magento system uses the same module system you are using to be built. Look at this

app/code/core/Mage

Each folder is a separate module created by Magento team and together modules form the shopping cart system you use today. So, you should place the folder by your modules as follows:

app/code/local/Packagename

“Packagename” should be unique string which is not being used by the world. The company name is an unofficial convention.

app/code/local/Microsoft

“Magento tutorial” is used in this post. You can add any module to the Magento system with following directory structure. It’s not necessary for you to include all of folders for every module but setting them up now is your intelligent idea.

app/code/local/Magentotutorial/Configviewer/Block

app/code/local/Magentotutorial/Configviewer/controllers

app/code/local/Magentotutorial/Configviewer/etc

app/code/local/Magentotutorial/Configviewer/Helper

app/code/local/Magentotutorial/Configviewer/Model

app/code/local/Magentotutorial/Configviewer/sql

Next, you need to create two files. The first file is config.xml, going to the etc folder you’ve just created.

app/code/local/Magentotutorial/Configviewer/etc/config.xml

The second one can be created as this location path. Conventional name of the file is Packagename_Modulename.xml

app/etc/modules/Magentotutorial_Configviewer.xml

The config.xml file should have the following XML. Don’t worry what it does now, we will get there.

<config>
<modules>
<Magentotutorial_Configviewer>
<version>0.1.0</version>
</Magentotutorial_Configviewer>
</modules>
</config>

At last, Magentotutorial_Configviewer.xml should contain xml as follows.

<config>
<modules>
<Magentotutorial_Configviewer>
<active>true</active>
<codePool>local</codePool>
</Magentotutorial_Configviewer>
</modules>
</config>

Congratulation, you have built a solid module that Magento is aware of. To make sure you’ve done right, follow the steps

  •      Clear Magento cache
  •      In Magento Admin Panel, System à Configuration à Advanced
  •       In the “Disable modules output” surface, check Magentotutorial_Configviewer to show up

2. Create A Module Config

At first, the module does nothing but when you are done with this part, the module will check for an appearance of a “showConfig” query string alternative. If showConfig is there, show our Magento config and stop normal performance. Besides, the module will check for any additional query string variable, showConfigFormat is for you to specify text or xml output.

Firstly, add the following <global> section to congif.xml file

<config>
<modules>…</modules>
<global>
<events>
<controller_front_init_routers>
<observers>
<Magentotutorial_configviewer_model_observer>
<type>singleton</type>
<class>Magentotutorial_Configviewer_Model_Observer</class>
<method>checkForConfigRequest</method>
</Magentotutorial_configviewer_model_observer>
</observers>
</controller_front_init_routers>
</events>
</global>
</config>

Then, create a file at

Magentotutorial/Configviewer/Model/Observer.php

Next, input the following code inside

<?php
class Magentotutorial_Configviewer_Model_Observer {
const FLAG_SHOW_CONFIG = ‘showConfig’;
const FLAG_SHOW_CONFIG_FORMAT = ‘showConfigFormat’;

private $request;

public function checkForConfigRequest($observer) {
$this->request = $observer->getEvent()->getData(‘front’)->getRequest();
if($this->request->{self::FLAG_SHOW_CONFIG} === ‘true’){
$this->setHeader();
$this->outputConfig();
}
}

private function setHeader() {
$format = isset($this->request->{self::FLAG_SHOW_CONFIG_FORMAT}) ?
$this->request->{self::FLAG_SHOW_CONFIG_FORMAT} : ‘xml’;
switch($format){
case ‘text’:
header(“Content-Type: text/plain”);
break;
default:
header(“Content-Type: text/xml”);
}
}

private function outputConfig() {
die(Mage::app()->getConfig()->getNode()->asXML());
}
}

That’s it. Clear your Magento cache again and load any Magento URL with a  showConfig=true query string

3. What to Look At?

You should look at a giant XML file that describes your Magento system state and lists all things you think of like modules, models, classes, event listeners or anything else. For example, considering the config.xml file created above to search the XML file in your browser for the text Configviewer_Model_Observer , you will find your listed class. Every config.xml file of module is parsed and included in the global config.

4. What To Take Care?

It seems esoteric but the config is key understanding of Magento. Every module you’re creating will add to this config, and anytime you access a piece of the core system functionality, Magento looks something up by referring back to the config.

A quick example is following

As an MVC developer, you’ve worked with helper classes and instantiate something like

$helper_sales = new HelperSales();

Magento has abstracted away PHP’s class declaration. In Magento, this code looks like

$helper_sales = Mage::helper(‘sales’);

In simple words, the static helper method will look into the <helpers /> section of the Config then look for <class /> section, after that append the part after the slash to the value found, finally instantiate the class just founded

This is a lot of work, the key advantage is looking to the config file for class names, you can override core Magento functionality without changing the core code. This level of meta programming allows you to extend parts of system you want cleanly.

app/code/local/Magentotutorial/Configviewer/etc/config.xml

Add Your Comment