The Magento Config
The config is the beating heart of the Magento System. It describe, in whole, almost any module/model/class/template/etc than you’ll need to access. It’s a level of abstraction that most PHP developers aren’t used to working with, and while it adds development time in the form of confusion and head scratching, it also allows you an unprecedented amount of flexibility as far as overriding default system behaviors go.
To start with, we’re going to create a Magento module that will let us view the system config in our web browser. While this is all wrote copying and pasting, it’s worth going through on your own as a way to start getting comfortable with things you’ll be doing while working with Magento, as well as learning key terminology.
For the impatient, the completed module can be downloaded here.
Setting up a module Directory Structure
We’re going to be creating a Magento module. A module is a group of php and xml files meant to extend or override core system behavior. This may meaning adding additional data models to track sales information, changing the behavior of existing classes, or adding entirely new features.
It’s worth noting that most of the base Magento system is built using the same module system you’ll be using. If you look in
app/code/core/Mage
each folder is a separate module built by the team at Varien. Together, these modules form the community shopping cart system you’re using. Your modules should be placed in the following folder
app/code/local/Packagename
“Packagename” should be a unique string to Namespace/Package your code. It’s an unofficial convention that this should be the name of your company. The idea is to pick a string that no one else if the world could possibly be using.
app/code/local/Microsoft
When I’m working on my own Magento projects, I use a version of my domain name, “Alanstormdotcom”.
So, to add a module to your Magento system, create the following directory structure
app/code/local/Alanstormdotcom/Configviewer/Block
app/code/local/Alanstormdotcom/Configviewer/controllers
app/code/local/Alanstormdotcom/Configviewer/etc
app/code/local/Alanstormdotcom/Configviewer/Helper
app/code/local/Alanstormdotcom/Configviewer/Model
app/code/local/Alanstormdotcom/Configviewer/sql
You won’t need all these folder for every module, but setting them all up now is a smart idea.
Next, there’s two files you’ll need to create. The first, config.xml, goes in the etc folder you just created.
app/code/local/Alanstormdotcom/Configviewer/etc/config.xml
The second file should be created at the following location
app/etc/modules/Alanstormdotcom_Configviewer.xml
The naming convention for this files is Packagename_Modulename.xml.
The config.xml file should contain the following XML. Don’t worry too much about what all this does for now, we’ll get there eventually
<config>
<modules>
<Alanstormdotcom_Configviewer>
<version>0.1.0</version>
</Alanstormdotcom_Configviewer>
</modules>
</config>
Finally, Alanstormdotcom_Configviewer.xml should contain the following xml.
<config>
<modules>
<Alanstormdotcom_Configviewer>
<active>true</active>
<codePool>local</codePool>
</Alanstormdotcom_Configviewer>
</modules>
</config>
That’s it, you now have a bare bones module that won’t do anything, but that Magento will be aware of. To make sure you’ve done everything right
- Clear your Magento cache
-
In the Magento Admin, go to
System->Configuration->Advanced - Expand “Disable modules output” (if it isn’t already)
- Ensure that Alanstormdotcom_Configviewer shows up
Congratulations, you’ve built your first Magento module!
Creating a Module Config
Of course, this module doesn’t do anything yet. When we’re done, our module will
- Check for the existence of a “showConfig” query string variable
- If showConfig is present, display our Magento config and halt normal execution
- Check for the existence of an additional query string variable, showConfigFormat that will let us specify text or xml output.
First, we’re going to add the following <global> section to our config.xml file.
<config>
<modules>...</modules>
<global>
<events>
<controller_front_init_routers>
<observers>
<alanstormdotcom_configviewer_model_observer>
<type>singleton</type>
<class>Alanstormdotcom_Configviewer_Model_Observer</class>
<method>checkForConfigRequest</method>
</alanstormdotcom_configviewer_model_observer>
</observers>
</controller_front_init_routers>
</events>
</global>
</config>
Then, create a file at
Alanstormdotcom/Configviewer/Model/Observer.php
and place the following code inside
<?php
class Alanstormdotcom_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 then load any Magento URL with a showConfig=true
query string
http://magento.example.com/?showConfig=true
What am I looking at?
You should be looking at a giant XML file. This describes the state of your Magento system. It lists all modules, models, classes, event listeners or almost anything else you could think of.
For example, consider the config.xml file you created above. If you search the XML file in your browser for the text
Configviewer_Model_Observer
you’ll find your class listed. Every module’s config.xml file is parsed by Magento and included in the global config.
Why Do I Care?
Right now this may seem esoteric, but this config is key to understanding Magento. Every module you’ll be creating will add to this config, and anytime you need to access a piece of core system functionality, Magento will be referring back to the config to look something up.
A quick example before we move on to more practical things. As an MVC developer, you’ve likely worked with some kind of helper class, instantiated something like
$helper_sales = new HelperSales();
One of the things Magento has done is abstract away PHP’s class declaration. In Magento, the above code looks something like
$helper_sales = Mage::helper('sales');
In plain english, the static helper method will
- Look in the <helpers /> section of the Config.
- Within <helpers />, look for a <sales /> section
- Within the <sales /> section look for a <class /> section
- Instantiate the class found in #3 (Mage_SalesRule_Helper)
While this seems like a lot of work (and it is), the key advantage is by always looking to the config file for class names, we can override core Magento functionality without changing or adding to the core code. This level of meta programming, not usually found in PHP, allows you to cleanly extend only the parts of the system you need to.
Like this article? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.