How to configure Zend_Application using application.ini and user.ini

I am using Zend_Application, and it does not seem to me that I am mixing application.ini in my application with both the configuration of the application and the user.

What I mean is the following. For example, my application needs some library classes in the MyApp_ namespace. So in application.ini I put autoloaderNamespaces [] = "MyApp_". This is the pure configuration of the application, no one but the programmer will change them. On the other hand, I set up the database configuration there, which will change SysAdmin.

My idea is that I would split the parameters between application.ini and user.ini, where preferences are given preference in user.ini (so I can define standard values ​​in application.ini).

Is that a good idea? How can I best implement this? I have an idea

  • Zend_Application extension for loading multiple configuration files
  • Executing the init function in my Bootstrap loading user.ini
  • Parsing configuration files in my index.php and passing them to Zend_Application (sounds ugly)

What should I do? I would like to have the “cleanest” solution ready for the future (new versions of ZF and other developers working in the same application)

+5
source share
6 answers

I found a solution to this problem, which may be new to version 1.10. When creating a Zend application object, you can pass two configuration file paths in an array of parameters that are combined together:

$application = new Zend_Application(
    APPLICATION_ENV,
    array(
        'config' => array(
            APPLICATION_PATH . '/configs/application.ini',
            APPLICATION_PATH . '/configs/user.ini'
        ),
    )
);
+9

, , ?

application.ini

[production]
config[] = APPLICATION_PATH "/configs/dsn.ini"
config[] = APPLICATION_PATH "/configs/error.ini"
...
+6

, - . . _initConfig(), Zend_Config_Ini. Zend_App, .

Edit:

:

$this->bootstrap('config');

, , - :

protected function _initConfig()
{
    $config = new Zend_Config_Ini('/path/to/user.ini');
    return $config;
}

protected function _initDb()
{
    $this->bootstrap('config');
    $config = $this->getResource('Config');

    /* ... */
}

Zend_Registry, , Bootstrap _init, getResource()

+5

"config", . Zend_Application . , .

Zend Framework

< >

application.ini:

[production]
config = APPLICATION_PATH "/configs/config.ini"
resources.db.adapter = "Mysqli"
resources.db.host = "localhost"

config.ini:

[production]
resources.db.host = "mysql.server.com"
resources.db.username = "myuser"

/index.php:

$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);
+1

, . , , config.ini

:

index.php

     $application = new Zend_Application(APPLICATION_ENV, array(
                'resources' => array(
                   'FrontController' => array(
                       'controllerDirectory' => APPLICATION_PATH . '/main/controllers',
                    ),
                'layout' => array(
                    'layoutpath' => APPLICATION_PATH . "/layouts/scripts"
                    ),
                ),
            ));

and then inside the bootstrap parse the config.ini inidependently

    protected function _initConfigFile() {
        try {
            $configuration = new Zend_Config_Ini(
                APPLICATION_PATH . '/config/app.ini',
                APPLICATION_ENV );
            $registry->configuration = $configuration;
        } catch (Zend_Exception $zExp) {
            echo "Could not read application ini file (app.ini). "
                . " Please check that it exists and has the appropriate structure.\n";
            echo("\n");
            var_dump($zExp);
            exit(1);
        }
    }

0
source

You can verify that the bootstrap method _initConfig () is called before others by specifying in other bootstrap methods (which require a configuration object):

$this->bootstrap('config');

A more complete example (context of the Bootstrap class):

protected function _initConfig() {
    $config = new Zend_Config_Ini('[filename]');
    Zend_Registry::set('config',$config);
}

protected function _initSomething() {
    $this->bootstrap('config');
    $config = Zend_Registry::get('config');
    // you can now do whatever you like with the $config object
}

Update:

As mentioned in other answers, if the configuration is only required in the bootstrap, I would say to use the method $this->getResource('Config'). I use the registry to easily access the configuration in other parts of my application.

-1
source

All Articles