Current application config
Created: Last updated:
Have you ever wondered how to get the current configuration from your application.ini file in your Zend Framework MVC project?
In this article I'm not only going to tell you where you get the configuration from but also how the Zend application put it there in the first place—plus as a bonus an interesting tip. Please also read the disclaimer at the bottom!
Follow the path
When you search for this question you will find many interesting answers but they all are a little bit weird; most of them don't match my understanding about the Zend Framework. So, I figured lets do some more research and dig through some code.
The first question I have is this one: "What happens with the application.ini file anyway?" When you have this answer it quickly leads to the solution for the initial question.
And here is the bonus tip I have promised: If you want to find the answer about something in the Zend Framework then just look at the code. After all, isn't that why we love OSS?
The config is an option
If you run your MVC Zend Framework based on the more or less default instruction then you will have the following line of code in your index.php file:
- // Create application, bootstrap, and run
- $application = new Zend_Application(
- APPLICATION_PATH . '/configs/application.ini' );
Now we simply follow the path in the code of the Zend_Application class and we will discover two things:
- the configuration gets stored in a "_option" property
- the Zend_Application object is passed into the bootstrap object
Second question: "What happens in the bootstrap class?"
Looking at our bootstrap.php file which extends down to the BootstrapAbstract.php file and the constructor we discover another thing:
- the configuration is stored in yet another "_option" property
This time it belongs to the bootstrap object, though.
Fetch the bootstrap
So, there you have it. All is left now is fetch your bootstrap and call the getOptions method. If you are in a controller you can use getInvokeArg and in other places you get the frontController first. Here's the full code
- // use this in a controller class
- $currentConfig = $this->getInvokeArg('bootstrap')->getOptions();
- // use this in other places
- $front = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getOptions()
Two important things to note and understand about this getOptions() method.
- The returned data is a regular PHP array and not a Zend_Config or Zend_Config_Ini object as you might suspect.
- You will get the current configuration only, i.e. the tree depending on your environment set with APPLICATION_ENV. Although Zend_Application reads the whole file it only adds the active configuration to the option property!
If you want to fetch the whole configuration, i.e. the whole content of the application.ini you will have to get the file itself with Zend_Config_Ini if it is an ini file.