1. The Magento Config

Created Tuesday 12 August 2014
http://alanstorm.com/magento_config

Setting up a module directory structure

A module is a group of php and xml files meant to extend or override core system behavior.

<config>
<modules>
<MyLearningModules_Configviewer>
<version>0.1.0</version>
</MyLearningModules_Configviewer>
</modules>
</config>
<config>
<modules>
<MyLearningModules_Configviewer>
<active>true</active>
<codePool>local</codePool>
</MyLearningModules_Configviewer>
</modules>
</config>

Creating a Module Config

When done, the module will:

  1. Check the existence of a "showConfig" query string variable
  2. If "showConfig" is present, display our Magento config and halt normal execution
  3. Check the existence of an additional query string variable "showConfigFormat" that will let specify text or xml output
  4. Add the <global> section to config.xml
<config>
<modules>...</modules>
<global>
<events>
<controller_front_init_routers>
<observers>
<mylearningmodules_configviewer_model_observer>
<type>singleton</type>
<class>MyLearningModules_Configviewer_Model_Observer</class>
<method>checkForConfigRequest</method>
</mylearningmodules_configviewer_model_observer>
</observers>
</controller_front_init_routers>
</events>
</global>
</config>
<?php
class MyLearningModules_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());
}
}



Backlinks: