07
Mar

Starting with Zend Framework – Configuration File and Database Connection

On the first article about the Zend Framework series, i have covered out the Setup of the Development Environment, on this article i will cover the Zend Framework configuration files and the database connection.

I will try to guide you on a brief and short path to start using the database in your Zend Framework project.

First Task – The configuration file

Our first task for today is to create the configurations file, i prefer to use php .ini files to save configuration settings, due to it’s clean and functional organization.

On your /public/config/ folder, create a file named configurations.ini with the following content.

[production]
database.adapter			=	pdo_mysql
database.params.host		=	localhost
database.params.username	=	root
database.params.password	=	xpto
database.params.dbname		=	restaurant
[development : production ]
database.params.host	=	127.0.0.1
database.params.username=	root
database.params.password=	ypto

I am defining my database settings to use PDO_Mysql driver and all the database needed settings, as you can see i am using two sections, the first one named [production] and the second one named [development:production] the : separation means that we are inheriting our development section from um previous production, and we will override some of the definitions. You can read more about this on the Zend Framework Config instructions.


Second Task: Load the configuration settings on your bootstrap file

Now that you have created the configuration file, you need to give some use tho it, lets go to the previously created bootstrap file, on your path /public/bootstrap.php, after the line that contains “Zend_Loader::registerAutoload();“, add the following content:

/**
 * Loading configuration from ini file
 */
$configuration = new Zend_Config_Ini(
    './config/configurations.ini', 
    'development'
);

We are reading the configuration file into a Zend_Config Object, to access the object you could by example do:

echo $config->database->params->host;

 

Third Task: Creating the database handler

Now that we have the configurations needed on a Zend_Config object we are ready to create the database connection. In order to do that, just after the previous code on bootstrap.php file, add the content.

/**
 * Creating the database handler from the loaded ini file
 */
$dbAdapter = Zend_Db::factory($configuration->database);
/**
 * Lets define the newly created handler as our default database handler
 */
Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter);
We are creating the database handler and defining the newly created database handler as the default for my front controller.

 Fourth Task: Using the Zend Registry to save the values

Now that we have our application using the database handler, and we are connected to the database, i will save the $configurations values and the $dbAdapter on the Registry for further usage.

/**
 * Lets add the configurations and the database handler to Registry
 * to use in future
 */
$registry = Zend_Registry::getInstance();
$registry->configuration = $configuration;
$registry->dbAdapter     = $dbAdapter;
/**
 * Now that we have the values on the Registry, lets cleanuo the variables
 * from the script scope
 */
unset($dbAdapter, $registry,$configuration);

You can read more about Zend Registry on the framework manual.

On this article, i have approached the Zend Config, Zend_Db and Zend_Registry usage, i are now able to deal with each one of this components, on my next article on this series, i will help you to create a Model extending the Zend_Db_Table.

Related posts:

  1. Starting with Zend Framework – Setup your development environment
  2. Create RESTful Application Using the Zend Framework
  3. Zend Framework: How-to interact with Google Calendar (Part 1/2)
  4. Zend Framework : Sending emails using Zend_Mail and Google Smtp
  5. Zend Framework TextMate Bundle

enjoyed this post? share with others:

twitter stumble upon digg

This entry was posted on Saturday, March 7th, 2009 at 12:33 am and is filed under Random Thoughts. You can follow any responses to this entry through the RSS 2.0 feed.

comments

3
  1. September 9th, 2011 | tudou99 says:

    Have you ever considered writing an e-book or guest authoring on other blogs? I’ve a weblog based on the same subjects you discuss and would adore to have you share some stories/information. I know my viewers would value your function. If you’re even remotely interested, feel totally free to shoot me an e-mail.

  2. March 13th, 2012 | Anes says:

    Hi Jose, nice job , but we freshers in zend framework need some complete code to test and run, expect like that from you soon.

    Thanks
    Anes

  3. May 1st, 2012 | pankaj says:

    Helo Jose, very good work. it’s really helpful

leave a comment