简体   繁体   中英

Zend Framework separating developer-specific environment info from config?

I am just getting started with a big Zend Framework project. One thing confuses me so far: much of the configuration seems to be stored in a single file at application/configs/application.ini . This file includes various things essential to the application's function such as app namespace, controller plugins, etc. and also the database login details. I have some external contractors that I need to get working on this project, but I don't want them to have all my database details. In Zend how do you separate the environment info such as db logins (which may be different for different developers) from the application config (which will be the same for everyone)?

I agree that this is more difficult in ZF than it should be, hopefully it's an area that will be improved in future versions.

In the meantime, it is quite easy to merge two config files with Zend_Config, and this is something you can use to your advantage. If you open public/index.php you'll see a section near the bottom where it creates an instance of Zend_Application. By default the second parameter is the full path to your config file, but you can pass in an existing Zend_Config object instead. So you create two config objects: application.ini and environment.ini (call this whatever you like), merge them together, and then pass this to Zend_Application:

$config = new Zend_Config_Ini(
    APPLICATION_PATH.'/configs/application.ini', 
    APPLICATION_ENV, 
    array('allowModifications' => true)
);
$environment = new Zend_Config_Ini(
    APPLICATION_PATH.'/configs/environment.ini', 
    APPLICATION_ENV
);

$config->merge($environment);

$application = new Zend_Application(APPLICATION_ENV, $config);
$application->bootstrap()
            ->run();

With this approach, you keep all the standard stuff in application.ini and move your database stuff to environment.ini. You then keep application.ini in source control, add environment.ini to gitignore/svn:ignore, and create a dummy environment.ini.dist which your other developers can use to setup their local projects.

Note: If you have commented out the require_once calls in ZF's library files for performance reasons, you may need to require in some of the Zend_Config classes in public/index.php to get this to work. This should be obvious from the errors though.

What i do for Symfony and Zend is not check in config files with info like that.

Instead ai make config.dist file which i keep in SVN/Git which has all the necessary info to run the app, and then dummy info for sensitive credentials and what not. Then on each machine (local dev, production, staging) i copy this file to the real cnfig file name and edit it with the appropriate environment specific details.

I am using PHP-based configuration files this way (application.php):

<?php
return array_merge_recursive(array(
    'bootstrap' => array(
        'phpSettings' => array(
            'display_startup_errors' => 0,
            'display_errors' => 0
        ),
    'path' => APPLICATION_PATH . '/Bootstrap.php',
        'class' => 'Bootstrap',
    )
    /** all other general config stuff as well **/
), include APPLICATION_PATH . '/configs/' . APPLICATION_ENV . '.php');

And one environment-specific one (for example development.php):

<?php
return array(
    'phpSettings' => array(
        'display_startup_errors' => 1,
        'display_errors' => 1,
        'error_reporting' => E_ALL,
    ),
    'resources' => array(
        /** log to browser only for development environment **/
        'log' => array(
            'Firebug' => array(
                'writerName' => 'Firebug',
                'filterName' => 'Priority',
                'filterParams' => array(
                    'priority' => Zend_Log::DEBUG
                )
            )
        ),
        'db' => array(
              /** Development Database settings, only need to overwrite the new ones **/
        )
    )
);

Idea stolen from this page (you'd put development.php on ignore then of course).

You could create two ini files in application/configs . The first file, application.ini contains most of your boilerplate configuration stuff. The second file, secrets.ini contains your database passwords and other things you don't want to check into source control. You're now able to check your application.ini file into source control. Don't check in secrets.ini -- get your VCS to ignore it -- and force each developer to create that file from scratch in their personal checkouts.

You could then write a method in your application/Bootstrap.php class to read both files, merge them together into a Zend_Config object, and write that object to Zend_Registry .

I don't think this solution is ideal, but it's worked for me in the past.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM