PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Quick Start

The following is a JSON version of a standard application configuration.

{
    "production":{
        "phpSettings":{
            "display_startup_errors": false,
            "display_errors": false
        },
        "includePaths":{
            "library": "APPLICATION_PATH/../library"
        },
        "bootstrap":{
            "path": "APPLICATION_PATH/Bootstrap.php",
            "class": "Bootstrap"
        },
        "appnamespace": "Application",,
        "resources":{
            "frontController":{
                "controllerDirectory": "APPLICATION_PATH/controllers",
                "moduleDirectory": "APPLICATION_PATH/modules",
                "params":{
                    "displayExceptions": false
                }
            },
            "modules":[],
            "db":{
                "adapter": "pdo_sqlite",
                "params":{
                    "dbname": "APPLICATION_PATH/../data/db/application.db"
                }
            },
            "layout":{
                "layoutPath": "APPLICATION_PATH/layouts/scripts/"
            }
        }
    },
    "staging":{
        "_extends": "production"
    },
    "testing":{
        "_extends": "production",
        "phpSettings":{
            "display_startup_errors": true,
            "display_errors": true
        },
    },
    "development":{
        "_extends": "production",
        "phpSettings":{
            "display_startup_errors": true,
            "display_errors": true
        },
        "resources":{
            "frontController":{
                "params":{
                    "displayExceptions": true
                }
            }
        }
    }
}

To utilize it, you simply instantiate Zend_Config_Json, pointing it to the location of this file and indicating the section of the file to load. By default, constant names found in values will be substituted with their appropriate values.

<?php
$config 
= new Zend_Config_Json(
    
APPLICATION_PATH '/configs/application.json',
    
APPLICATION_ENV
);

Once instantiated, you use it as you would any other configuration object.

<?php
$db 
Zend_Db::factory($config->resources->db);

Use Constants With Care

JSON has a strict structure with regards to data types. As such, you need to ensure that your constants are use correctly. For constants that have string values, put your constant values in double quotes (""). For non-string values, you can omit the quotes -- but be absolutely certain that they are not returning strings, as otherwise you will encounter parser errors with your configuration file. When in doubt, enclose the contant in double quotes.

Zend Framework