Configuring PHP ini Settings in ZF2

Configuring PHP ini Settings in ZF2

Spoiled by the ease with which it was possible to configure the PHP ini directives in Zend Framework 1 (in application.ini) I was hoping for a similar facility in ZF2. As far as I know, there is none (as of Beta 4). The remedy is easy though.

This post has been updated to reflect the changes in Zend Framework 2 Beta 4

The task

We want to be able to configure the PHP ini settings in our ZF2 application, integrate the process naturally into the application workflow and use the existing configuration infrastructure (i.e. merging of configuration files, global / local overrides, etc.). Let’s build a ZF2 module for this!

The module

We should rather say ‘a mini-module’ as it really needs just a single file – Module.php. Let’s call this module PhpSettings. Here is its Module class:

<?php
namespace PhpSettings;

use Zend\EventManager\Event;

class Module
{
    /**
     * Configure PHP ini settings on the bootstrap event
     * @param Event $e
     */
    public function onBootstrap(Event $e) {
        $app         = $e->getParam('application');
        $config      = $app->getConfiguration();
        $phpSettings = $config['phpSettings'];
        if($phpSettings) {
            foreach($phpSettings as $key => $value) {
                ini_set($key, $value);
            }
        }
    }

    /* The getAutoloaderConfig() and getConfig() methods are left out here
       for brevity, as they are completely standard.
     */
}

The public method onBootstrap() is automatically recognized as a bootstrap event listener (a new feature in Beta4 – in Beta3 we had to register the listener in the init() method manually). It retrieves the configuration and iterates over its phpSettings entries passing them to the standard PHP ini_set() function.

Do not forget to enable your new module in your /config/application.config.php.

Configuration

The only thing left is to let our module know what directives we want to set. Just place phpSettings key into a configuration file and put all required PHP ini directives as its subkeys. You can find the list of all available PHP ini directives in the PHP docs.

You should follow the global/local concept of ZF2 configuration. NOTE: The naming of the default global / local configuration files changed in Beta4 – the ‘config’ part of the file name has been omitted. Watch out for this if your good old config files in the autoload directory stopped working after migration to Beta4.

Place your global environment agnostic options into global configuration files (e.g. /config/autoload/module.DluPhpSettings.global.php). These files will be included in your VCS. Here is an example of a global configuration:

<?php
/**
 * My Global Configuration
 * /config/autoload/module.DluPhpSettings.global.php
 *
 * You can use this file for overridding configuration values from modules, etc.  
 * You would place values in here that are agnostic to the environment and not 
 * sensitive to security. 
 *
 * @NOTE: In practice, this file will typically be INCLUDED in your source 
 * control, so do not include passwords or other sensitive information in this 
 * file.
 */

return array(
    'phpSettings'   => array(
        'display_startup_errors'        => false,
        'display_errors'                => false,
        'max_execution_time'            => 60,
        'date.timezone'                 => 'Europe/London',
        'mbstring.internal_encoding'    => 'UTF-8',
    ),
);

Your environment specific or sensitive information should go into local configuration files (e.g. /config/autoload/module.DluPhpSettings.local.php). Values in these files override the global configuration and they are excluded from the version control by default (if you are using the Zend Skeleton Application).

Here is an example of a local configuration present on a developer’s machine. It overrides some values from the global configuration:

<?php
/**
 * My Local Configuration (Development)
 * /config/autoload/module.DluPhpSettings.local.php
 *
 * This configuration override file is for overriding environment-specific and
 * security-sensitive configuration information.
 *
 * @NOTE: This file is ignored from Git by default with the .gitignore included
 * in ZendSkeletonApplication. This is a good practice, as it prevents sensitive
 * credentials from accidentally being comitted into version control.
 */

return array(
    'phpSettings'   => array(
        'display_startup_errors'        => true,
        'display_errors'                => true,
        'max_execution_time'            => 600,
    ),
);

Conclusion

We have created an ultra simple module which cleanly sets the PHP environment for our application. If you do not feel like coding even the single class needed for this module, you can download the DluPhpSettings module from my git repository.

How do YOU configure your PHP environment in your ZF2 applications? Let me know.