Welcome to Abdul Malik Ikhsan's Blog

Zend Framework 2 : Setting Up Module Config using XML File

Posted in Tutorial PHP, Zend Framework 2 by samsonasik on October 6, 2012

You have bored with nested array configuration ? Zend Framework 2 give us not only one choice to setting up module config, not only array, we can use another way : XML, ini, Yaml. I will show you how to use xml file be a config of your module.

First, create your xml file like the following :

<?xml version="1.0" encoding="UTF-8"?>


Save it in YourModule/config/module.config.xml. So, we can call it in Module class :

namespace SampleModule;

use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;

class Module implements
    public function getAutoloaderConfig(){/*common code*/}

    public function getConfig()
	$reader = new \Zend\Config\Reader\Xml();
	$data   = $reader->fromFile(__DIR__ . '/config/module.config.xml');

	$config = new \Zend\Config\Config(array('__DIR__' => __DIR__.'/config'), true);
	$processor = new \Zend\Config\Processor\Constant();

	$data['view_manager']['template_path_stack']['SampleModule'] = $config->__DIR__.'/../view';

        return $data;

You can use \Zend\Config\Reader\Xml file to read module.config.xml. One thing that we can’t write in xml file is __DIR__.’/../view’ in view_manager template_path. It should be in Module.php.

Conclusion :
1. You can use other format for module config.
2. Using XML file need to be “parsing” into array, this is not good for performance.
3. Using XML file can make code readable for some people, just choose yours.

Done !

Tagged with:

16 Responses

Subscribe to comments with RSS.

  1. Matthew Weier O'Phinney said, on October 9, 2012 at 8:38 am

    While you cannot use magic constants by default, you _can_ use them if you use the Config “constants” processor: http://zf2.readthedocs.org/en/latest/modules/zend.config.processor.html#zend-config-processor-constant — this could be easily added to the consuming Module.

  2. samsonasik said, on October 9, 2012 at 12:46 pm

    Updated, cmiiw 😉

  3. Abdullah said, on October 16, 2012 at 6:03 pm

    جزاك الله خير الدرس ممتاز

  4. samsonasik said, on October 16, 2012 at 9:22 pm


  5. John said, on April 21, 2013 at 5:43 am

    ZF2 configuration is very robust, but its really a productivity killer, someone needs to build us a tool to achieve this a lot faster.

  6. kalelc said, on July 16, 2013 at 5:21 am

    hello. I would like to know what is the best way to draw file containing module name, url and other descriptions, with config.ini file or through global.php and local.php with ServiceManager?

  7. Valerio Masciotta said, on February 13, 2014 at 6:24 am

    “Using XML file need to be “parsing” into array, this is not good for performance.” there isn’t a way to cache it?

    • samsonasik said, on February 16, 2014 at 4:28 pm

      xml is just an option, if you want better performance, use php array instead.

      • Valerio Masciotta said, on February 18, 2014 at 7:43 am

        Thx for your reply. I started to learn ZF2 not a long ago and I’m trying to understand. Based on your article “Zend Framework 2 : Cache Module Config and Module Classmap”, if you enable cache like You wrote there, You will get of course a config array that will bypass all the xml parsing. Don’t we solve the performance problem in this way?

  8. Webdevilopers (@webdevilopers) said, on May 8, 2014 at 6:04 pm

    Is it possible to move only the routing configuration from my module.config.php into e.g. a separated XML file?

    • samsonasik said, on May 9, 2014 at 6:17 am

      you can do array_merge and split into files, for ex : array for service registration, xml for routing…

  9. Webdevilopers (@webdevilopers) said, on May 9, 2014 at 1:59 pm

    Good hint, thanks!

  10. Webdevilopers (@webdevilopers) said, on July 14, 2014 at 9:15 pm

    Can I use ‪#‎zf2‬ ‪#‎translation‬ with ‪#‎yaml‬? I would like to import existing .yml files from a ‪#‎Ruby‬ project into my Translation. Can this be achieved e.g. with the #Yaml ‪#‎Config‬ Reader?

    Maybe implement the #Symfony2 #YAML component into #zf2? http://symfony.com/…/components/yaml/introduction.html – I think it is also used with #DoctrineORM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: