Using INI configuration files for Zend Framework 2

We’re exploring Zend Framework 2 at work, and one of the things we aren’t exciting about is configuration files full of arrays. We’re more comfortable with ini based configuration files.

Application config:

The main configuration file is in config/application.config.php

As a config file it becomes application.config.ini


In my public/index.php I needed to tell Zend how to read this config file:

There are also readers for Xml, Json, and Yaml that all work similarly.

Autoload Config Files

I changed the application.config.ini to search for ini files instead of php files:

I didn’t need to do anything with a reader object. Zend must see that they are ini files and passes them to the appropriate reader.

Module Config Files

Finally, I can do the same inside a module. In my Module.php I changed the getConfig() method to:

public function getConfig() {
$reader = new \Zend\Config\ReaderIni();
return $reader->fromFile(__DIR__ . ‘/config/module.config.ini’);
}

Further Reading:

Zend Config Reader documentation

Zend Config Factory Documentation – This has a method for returning the correct reader regardless of what file you pass in.

 

This entry was posted in Neat Tricks, Zend Framework 2. Bookmark the permalink.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.