Accessing custom values from your Application.ini anywhere in your application using Zend_Registry

In most applications you need to have some settings that control how the application will behave. Variables that can be changed to tweak how some specific thing is handled. In Zend Framework 1.10 this is pretty straightforward. I’m here going to show you how I solved this problem. I’m sure there are many ways to accomplish this but this is the way I used.

You create your custom variables in your Application.ini

; DB Settings
resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = secret
resources.db.params.dbname = gamedb

; My Settings
GameSettings.startCredits = 100
GameSettings.startPos.y = 288
GameSettings.startPos.x = 10

; Misc Settings
resources.frontController.params.displayExceptions = 0
resources.view.helperPath = APPLICATION_PATH “/views/helpers”
phpSettings.date.timezone = "Europe/London"

and then you use the Zend_Registry to make the config settings available anywhere in your application by adding the following code to your Bootstrap.php

protected function _initConfig() {

        Zend_Registry::set('config', $this->getOptions());

}

In your application model, controller or view you can now access your settings like this:

//Get config values
$config = Zend_Registry::get('config');

$startCredits  = $config['GameSettings']['startCredits'];
$startPosY     = $config['GameSettings']['startPos']['y'];
$startPosX     = $config['GameSettings']['startPos']['x'];

You can also include the config object in you init() method to have it easily accessible in your class

class MyClass extends Zend_Something {

       protected $_gameSettings = array();

       public function init() {

               $config = Zend_Registry::get('config');

               $this->_gameSettings = $config;

       }
...
}
  1. hey man … you save my time!! awesome thank you

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">

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