{"id":25,"date":"2012-02-27T21:25:06","date_gmt":"2012-02-27T21:25:06","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/2012\/02\/json_config_files_in_zend_framework-shtml\/"},"modified":"2012-02-27T21:25:06","modified_gmt":"2012-02-27T21:25:06","slug":"json_config_files_in_zend_framework-shtml","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/json_config_files_in_zend_framework-shtml\/","title":{"rendered":"JSON Config Files In Zend Framework"},"content":{"rendered":"<p>\nDuring a recent code review it was suggested that we should move from using an ini file to a JSON file for our Zend Framework application config file. The reason was to reduce duplication of data that can occur and instead move to a nicely nested file.\n<\/p>\n<p>\nI wasn&#8217;t aware that JSON format config files were supported, but sure enough after a quick look at the Zend Framework documentation, there was the relevant class, <a href=\"http:\/\/framework.zend.com\/manual\/1.11\/en\/zend.config.adapters.json.html\">Zend_Config_Json<\/a>. It has only appeared in recent versions of the Zend Framework so that could be why I had missed it.\n<\/p>\n<p>\nIt&#8217;s easy to use in code, being almost a complete drop in for a the usual ini config.\n<\/p>\n<pre class=\"lang:php decode:true \" >$config = new Zend_Config_Json(\n  '.\/example_config.json',\n  'production'\n);<\/pre>\n<p>\nThe first parameter is the location of the config file, the second is the section of the config file to use.\n<\/p>\n<p>\nThere is an optional third paramter that allows some addition configuration options to be specified such as changing the immutability of the config object once loaded, changing the behaviour of <code>_extends<\/code>, and the ability to ignore constants. More details can be found in <a href=\"http:\/\/framework.zend.com\/manual\/1.11\/en\/zend.config.adapters.json.html#zend.config.adapters.json.options\">options section of the Zend_Config_Json manual<\/a>.\n<\/p>\n<p>\nHere&#8217;s a quick example. Firstly the JSON configuration file.\n<\/p>\n<pre class=\"lang:js decode:true \" >{\n  \"production\" : {\n    \"username\" : \"Robert Price\",\n    \"beer\" : [\n      \"Harveys Sussex Best\",\n      \"Spitfire\",\n      \"Asahi\"\n    ]\n  },\n  \"testing\" : {\n    \"_extends\" : \"production\",\n    \"beer\" : [\n      \"Harveys Star Of Eastbourne\",\n      \"Schwaben Brau - Das Helle\",\n      \"Brauhaus J.Albrect - Dunkel\"\n    ]\n  }\n}\n<\/pre>\n<p>OK, it&#8217;s a good real life example, but it has two sections, <var>production<\/var> and <var>testing<\/var>. Testing inherits from <var>production<\/var> by using the <var>_extends<\/var> parameter.<br \/>\nWe can load and extract details using the following code.<\/p>\n<pre class=\"lang:php decode:true \" >$config = new Zend_Config_Json(\n  '.\/example_config.json',\n  'production'\n);\necho \"Username is \" . $config-&gt;username . \"n\";\necho \"Favourite beers are...n\";\nforeach ($config-&gt;beer as $beer) {\n  echo \"* \" . $beer . \"n\";\n}\n<\/pre>\n<p>\nWhen you run this code you get the following results, assuming you saved the json config file from earlier as <var>example_config.json<\/var>&#8230;\n<\/p>\n<pre class=\"lang:sh decode:true \" >Username is Robert Price\nFavourite beers are...\n* Harveys Sussex Best\n* Spitfire\n* Asahi\n<\/pre>\n<p>\nNow change the code so instead of <var>production<\/var> when we load the config file, we use <var>testing<\/var>. Run the code and you&#8217;ll get the following&#8230;\n<\/p>\n<pre class=\"lang:sh decode:true \" >Username is Robert Price\nFavourite beers are...\n* Harveys Star Of Eastbourne\n* Schwaben Brau - Das Helle\n* Brauhaus J.Albrect - Dunkel\n<\/pre>\n<p>\nAs you can see my <var>username<\/var> has been inherited from the production block, but the list of beers has changed. This is a very powerful feature that lets you change a small section of data between environments in real code without having to repeat yourself and reduces the risk introducing errors in duplicated config options.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>During a recent code review it was suggested that we should move from using an ini file to a JSON file for our Zend Framework application config file. The reason was to reduce duplication of data that can occur and instead move to a nicely nested file. I wasn&#8217;t aware that JSON format config files &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/json_config_files_in_zend_framework-shtml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;JSON Config Files In Zend Framework&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[50,81],"class_list":["post-25","post","type-post","status-publish","format-standard","hentry","category-dev","tag-php","tag-zend-framework"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>JSON Config Files In Zend Framework - Robert Price<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/json_config_files_in_zend_framework-shtml\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JSON Config Files In Zend Framework - Robert Price\" \/>\n<meta property=\"og:description\" content=\"During a recent code review it was suggested that we should move from using an ini file to a JSON file for our Zend Framework application config file. The reason was to reduce duplication of data that can occur and instead move to a nicely nested file. I wasn&#8217;t aware that JSON format config files &hellip; Continue reading &quot;JSON Config Files In Zend Framework&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/json_config_files_in_zend_framework-shtml\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2012-02-27T21:25:06+00:00\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/json_config_files_in_zend_framework-shtml\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/json_config_files_in_zend_framework-shtml\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"JSON Config Files In Zend Framework\",\"datePublished\":\"2012-02-27T21:25:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/json_config_files_in_zend_framework-shtml\\\/\"},\"wordCount\":343,\"keywords\":[\"PHP\",\"Zend Framework\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/json_config_files_in_zend_framework-shtml\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/json_config_files_in_zend_framework-shtml\\\/\",\"name\":\"JSON Config Files In Zend Framework - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"datePublished\":\"2012-02-27T21:25:06+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/json_config_files_in_zend_framework-shtml\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/json_config_files_in_zend_framework-shtml\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/json_config_files_in_zend_framework-shtml\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JSON Config Files In Zend Framework\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JSON Config Files In Zend Framework - Robert Price","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.robertprice.co.uk\/robblog\/json_config_files_in_zend_framework-shtml\/","og_locale":"en_GB","og_type":"article","og_title":"JSON Config Files In Zend Framework - Robert Price","og_description":"During a recent code review it was suggested that we should move from using an ini file to a JSON file for our Zend Framework application config file. The reason was to reduce duplication of data that can occur and instead move to a nicely nested file. I wasn&#8217;t aware that JSON format config files &hellip; Continue reading \"JSON Config Files In Zend Framework\"","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/json_config_files_in_zend_framework-shtml\/","og_site_name":"Robert Price","article_published_time":"2012-02-27T21:25:06+00:00","author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/json_config_files_in_zend_framework-shtml\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/json_config_files_in_zend_framework-shtml\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"JSON Config Files In Zend Framework","datePublished":"2012-02-27T21:25:06+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/json_config_files_in_zend_framework-shtml\/"},"wordCount":343,"keywords":["PHP","Zend Framework"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/json_config_files_in_zend_framework-shtml\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/json_config_files_in_zend_framework-shtml\/","name":"JSON Config Files In Zend Framework - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2012-02-27T21:25:06+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/json_config_files_in_zend_framework-shtml\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/json_config_files_in_zend_framework-shtml\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/json_config_files_in_zend_framework-shtml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"JSON Config Files In Zend Framework"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/25","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=25"}],"version-history":[{"count":0,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/25\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=25"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=25"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}