sensimevanidus

20 July, 2010

Fetching Configuration Data from Action Controllers in Zend Framework

If you’re familiar with Zend Framework, you probably know what an application-specific configuration file is. I tend to use application.ini file (located under the /application/configs directory) to store my application’s configuration data and generally use these variables in the bootstrap file.

Today, I needed to reach smtp configuration values from within a controller and it took some time to figure out how to do that although it was simple as this:

$mailOptions = $this->getInvokeArg('bootstrap')->getOption('mail');

This code snippet fetches these mail configuration data; 

mail.transport.smtp.host = "<some_host_value>"
mail.transport.smtp.port = "<some_port_value>"
mail.transport.smtp.user = "<some_user_value>"
mail.transport.smtp.pass = "<some_pass_value>"

from the application.ini file as a PHP array:

array(1) {
["transport"]=>
array(1) {
["smtp"]=>
array(4) {
["host"]=>
string(19) "<some_host_value>"
["port"]=>
string(3) "<some_port_value>"
["user"]=>
string(19) "<some_user_value>"
["pass"]=>
string(7) "<some_pass_value>"
}
}
}

You can also fetch all configuration data via:

$options = $this->getInvokeArg('bootstrap')->getOptions();

That’s all! Hope that helps…