Posted by Peter

Zend Framework series #1

How to use options from additional config ini file? Pretty easy actually :)

  1. create new file in application\configs directory. Name it something like extra_parameters.ini

  2. you can place here any type of options and I mean ANY:

    • list of access levels of your app
    • information if debug is enabled (or other flag you wish to use)
    • or any of available Zend config parameters
    • for example:
    1
    2
    
      colors[] = blue
      colors[] = red
    

    or

    1
    
      resources.session.name = "my_session"
    

    and so on, and so on… :)

  3. As you might already know Zend Framework recognizes various Application Environments, mostly used are Production and Development, but you might want to use few of your own. Each set of options in INI file must belong to one of these environments. you can define it like this:

    1
    2
    3
    4
    5
    6
    
      [production]
      ; some options
    
      [development : production]
    
      ; some more options
    

    By the way: you can define that one environment will inherit it’s options from another, in my example “development” inherit’s from “production”.

  4. Now, that you have your options and parameters, it’s time to get to them from our application. I will access my config from controller. It is basically few lines of code and it gives you full (read) access to config file, as follows:

    1
    2
    
    <?php
    $options = new Zend_Config_Ini('../application/configs/extra_parameters.ini', APPLICATION_ENV);
    

    APPLICATION_ENV - this will make Zend read only appropriate section from config

    now in order to access particular option do something like this (if it is an array):

    1
    2
    
    <?php
    $some_option = $options-&amp;gt;your_option-&amp;gt;toArray();
    

    or like this:

    1
    2
    
    <?php
    $some_option = $options-&amp;gt;your_option;
    
  5. And that’s pretty all. From now on you can use these values as any other PHP variables/arrays, maybe pass them to the view… you options are limitless ;)