Zend Framework Config: Using Array Configuration


Zend Framework Configuration Step By Step Tutorial - Part 1: There are important information where we build a database application such as host of database, database name, user, database, name of application, and so on. These informations are retrieved extensively. It will concise if we put them on a configuration part. Zend Framework have zend_config, is designed to simplify access to and use of configuration data within applications.

In this post, we see how to implement zend_config use array. For case, see our code from previous tutorial:

<?php
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', true);
date_default_timezone_set('Europe/London');

$rootDir = dirname(dirname(__FILE__));
set_include_path($rootDir . '/library' . PATH_SEPARATOR . get_include_path());


require_once 'Zend/Controller/Front.php';
require_once 'Zend/Registry.php';
require_once 'Zend/Db/Adapter/Pdo/Mysql.php';

Zend_Registry::set('title',"My First Application");

$arrName = array('Ilmia Fatin','Aqila Farzana', 'Imanda Fahrizal');
Zend_Registry::set('credits',$arrName);

$params = array('host'		=>'localhost',
	                'username'	=>'root',
					'password'  =>'admin',
					'dbname'	=>'zend'
	               );
$DB = new Zend_Db_Adapter_Pdo_Mysql($params);
    
$DB->setFetchMode(Zend_Db::FETCH_OBJ);
Zend_Registry::set('DB',$DB);



Zend_Controller_Front::run('../application/controllers');

?>

That is index.php. Location code above at web_root. Line 14 and 19-23 often use along application. We can put into an array configuration like this:

<?php
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', true);
date_default_timezone_set('Europe/London');

$rootDir = dirname(dirname(__FILE__));
set_include_path($rootDir . '/library' . PATH_SEPARATOR . get_include_path());


require_once 'Zend/Controller/Front.php';
require_once 'Zend/Registry.php';
require_once 'Zend/Db/Adapter/Pdo/Mysql.php';
require_once 'Zend/Config.php';


$arrConfig = array(
      'webhost'=>'localhost',
	  'appName'=>'My First Zend',
	  'database'=>array(
	      'dbhost'=>'localhost',
		  'dbname'=>'zend',
		  'dbuser'=>'root',
		  'dbpass'=>'admin'
	      )
      );

$config = new Zend_Config($arrConfig);

$title  = $config->appName;
$params = array('host'		=>$config->database->dbhost,
	            'username'	=>$config->database->dbuser,
			    'password'  =>$config->database->dbpass,
				'dbname'	=>$config->database->dbname
	            );


Zend_Registry::set('title',$title);

$arrName = array('Ilmia Fatin','Aqila Farzana', 'Imanda Fahrizal');
Zend_Registry::set('credits',$arrName);

$DB = new Zend_Db_Adapter_Pdo_Mysql($params);
    
$DB->setFetchMode(Zend_Db::FETCH_OBJ);
Zend_Registry::set('DB',$DB);



Zend_Controller_Front::run('../application/controllers');

?>

First, load Zend/Config.php (see line 13). Then create array that contains configuration (see line 16-25). Next, Create the object-oriented wrapper upon the configuration data (see line 27). Last, example access configuration data see line 29-34.



Series this article:
Zend Framework Config: Using Array Configuration
Zend Framework Config: Creating File Configuration
Zend Framework Config: Using INI File Configuration
Zend Framework Config: Using XML File Configuration

Bookmark and Share Tag: framework, zend, zend framework, configuration Category: PHP Framework Post : April 21st 2008 Read: 17,209

blog comments powered by Disqus