Magento: config file mapping

Just had a moment of epiphany that I thought I’d share…I’m sure this is well known to most Magento developers, but I’ve not seen it clearly spelled out anywhere and it’s taken me a while to get my head round this.

Have a look at this snippet of a config file for one of my modules:

 

 

 

 

 

 

Now, notice the lines that say ‘<CustomAttributes>’.  I’ve seen various explanations of what should go in here ranging from ‘it should match the name of your module’ to ‘it doesn’t matter’.  However, I’ve finally figured out what that element is used for.  It’s a mapping string, that maps your classes to an easily used string.

Once I’ve defined my mapping string as ‘CustomAttributes’ magento has a registry that allows me to call my helpers as follows:

Mage::helper(‘CustomAttributes/helper1’);

This would return the helper located at ‘TallPaul/Customattributes/Helper/helper1.php’.

The same goes for blocks (using something like createBlock) and so on.  It’s probably recommended that you use ‘yournamespace_yourmodule’ as the mapping string, to avoid conflicts, but technically speaking you can use anything you like.

This is confused slightly by the fact that Magento uses a lot of default values in its own code so you’ll rarely see helpers called using mapping strings that make sense.  For example the default helper is called ‘Data.php’.  So when magento makes a call like ‘Mage::helper(‘catalog’); it’s actually asking for ‘Mage_Catalog_Helper_Data.php’.

 

 

 




No Comments


You can leave the first : )



Leave a Reply

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