{"id":104,"date":"2013-06-19T23:12:39","date_gmt":"2013-06-19T23:12:39","guid":{"rendered":"http:\/\/burnhamup.com\/blog\/?p=104"},"modified":"2013-06-20T23:28:00","modified_gmt":"2013-06-20T23:28:00","slug":"using-ini-configuration-files-for-zend-framework-2","status":"publish","type":"post","link":"https:\/\/burnhamup.com\/blog\/2013\/06\/using-ini-configuration-files-for-zend-framework-2\/","title":{"rendered":"Using INI configuration files for Zend Framework 2"},"content":{"rendered":"<p>We&#8217;re exploring Zend Framework 2 at work, and one of the things we aren&#8217;t exciting about is configuration files full of arrays. We&#8217;re more comfortable with ini based configuration files.<\/p>\n<h2>Application config:<\/h2>\n<p>The main configuration file is in config\/application.config.php<\/p>\n<pre>&lt;?php\r\nreturn array(\r\n     'modules' =&gt; array(\r\n         'Application',\r\n         'Album'\r\n      ),\r\n\r\n      'module_listener_options' =&gt; array(\r\n           'module_paths' =&gt; array(\r\n                '.\/module',\r\n                '.\/vendor',\r\n            ),\r\n           'config_glob_paths' =&gt; array(\r\n                'config\/autoload\/{,*.}{global,local}.php',\r\n                ),\r\n           ),\r\n       ),\r\n);<\/pre>\n<div><\/div>\n<div>As a config file it becomes application.config.ini<\/div>\n<div>\n<pre>modules[] = Application\r\nmodules[] = Album\r\nmodules[] = PhlyContact<\/pre>\n<pre>module_listener_options.module_paths[] = .\/module\r\nmodule_listener_options.module_paths[] = .\/vendor\r\nmodule_listener_options.config_glob_paths[] = config\/autoload\/{,*.}{global,local}.php<\/pre>\n<p>In my public\/index.php I needed to tell Zend how to read this config file:<\/p>\n<pre>$reader = new Zend\\Config\\Reader\\Ini();\r\n<span style=\"font-family: 'Courier 10 Pitch', Courier, monospace; font-size: 13px; font-style: normal; line-height: 1.5;\">\r\n\/\/ Run the application!<\/span>\r\n<em id=\"__mceDel\">Zend\\Mvc\\Application::init($reader-&gt;fromFile('config\/application.config.ini'))-&gt;run();<\/em><\/pre>\n<p>There are also readers for Xml, Json, and Yaml that all work similarly.<\/p>\n<h2>Autoload Config Files<\/h2>\n<p>I changed the application.config.ini to search for ini files instead of php files:<\/p>\n<pre>module_listener_options.config_glob_paths[] = config\/autoload\/{,*.}{global,local}.ini<\/pre>\n<p>I didn&#8217;t need to do anything with a reader object. Zend must see that they are ini files and passes them to the appropriate reader.<\/p>\n<h2>Module Config Files<\/h2>\n<p>Finally, I can do the same inside a module. In my Module.php I changed the getConfig() method to:<\/p>\n<p>public function getConfig() {<br \/>\n$reader = new \\Zend\\Config\\ReaderIni();<br \/>\nreturn $reader-&gt;fromFile(__DIR__ . &#8216;\/config\/module.config.ini&#8217;);<br \/>\n}<\/p>\n<h2>Further Reading:<\/h2>\n<p><a title=\"Zend Config Reader documentation\" href=\"http:\/\/framework.zend.com\/manual\/2.0\/en\/modules\/zend.config.reader.html\">Zend Config Reader documentation<\/a><\/p>\n<p><a href=\"http:\/\/framework.zend.com\/manual\/2.0\/en\/modules\/zend.config.factory.html\">Zend Config Factory Documentation<\/a>\u00a0&#8211; This has a method for returning the correct reader regardless of what file you pass in.<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>We&#8217;re exploring Zend Framework 2 at work, and one of the things we aren&#8217;t exciting about is configuration files full of arrays. We&#8217;re more comfortable with ini based configuration files. Application config: The main configuration file is in config\/application.config.php &lt;?php &hellip; <a href=\"https:\/\/burnhamup.com\/blog\/2013\/06\/using-ini-configuration-files-for-zend-framework-2\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[10,13],"tags":[],"_links":{"self":[{"href":"https:\/\/burnhamup.com\/blog\/wp-json\/wp\/v2\/posts\/104"}],"collection":[{"href":"https:\/\/burnhamup.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/burnhamup.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/burnhamup.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/burnhamup.com\/blog\/wp-json\/wp\/v2\/comments?post=104"}],"version-history":[{"count":16,"href":"https:\/\/burnhamup.com\/blog\/wp-json\/wp\/v2\/posts\/104\/revisions"}],"predecessor-version":[{"id":122,"href":"https:\/\/burnhamup.com\/blog\/wp-json\/wp\/v2\/posts\/104\/revisions\/122"}],"wp:attachment":[{"href":"https:\/\/burnhamup.com\/blog\/wp-json\/wp\/v2\/media?parent=104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/burnhamup.com\/blog\/wp-json\/wp\/v2\/categories?post=104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/burnhamup.com\/blog\/wp-json\/wp\/v2\/tags?post=104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}