Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Configuration and environments

Laminas configuration is a set of PHP files, each returning an array. On startup the framework merges them into one big array. An environment is the place where the site runs: your computer or the production server. Environments have different passwords and treat errors differently, so the configuration is split across several files.

Where configuration comes from

FileWhat it holdsGoes into Git
module/*/config/module.config.phpConfiguration of one moduleYes
config/autoload/global.phpSettings shared by the whole projectYes
config/autoload/local.phpPasswords and keys of this machineNo

The files are merged in this order: first the modules as listed in config/modules.config.php, then global.php, then local.php. When the same key appears twice, the file read later wins.

Shared and local settings

Add a section of your own to config/autoload/global.php:

// config/autoload/global.php
return [
    'blog' => [
        'title' => 'My blog',
        'posts_per_page' => 10,
    ],
];

The section name blog is our own invention: you can put any keys you like into the configuration array.

Create local.php by hand next to it (the skeleton has a sample, local.php.dist). It overrides only what differs on this machine:

// config/autoload/local.php
return [
    'blog' => [
        'posts_per_page' => 3,
    ],
];

After merging, title is still "My blog" and posts_per_page equals 3.

Reading the configuration

The whole array is stored in the service container under the name config. The container is passed to factories — classes that create objects; the service manager lesson covers them. Inside a factory a setting is read like this:

$config = $container->get('config');
 
$perPage = $config['blog']['posts_per_page'];

The $perPage variable holds the number 3.

Development mode

The composer development-enable command copies two sample files:

SampleCopyWhat it changes
config/development.config.php.distconfig/development.config.phpTurns the configuration cache off
config/autoload/development.local.php.distconfig/autoload/development.local.phpShows error details on the page

composer development-disable removes the copies, and the site goes back to production mode.

Your own config files

You can create other files in config/autoload. Laminas reads every file whose name ends with global.php or local.php, for example db.global.php and db.local.php. This keeps the settings of each topic separate.

Common mistake

A database password written in global.php ends up in the repository, visible to everyone who has access to the code. Put passwords and keys only into local.php: the skeleton's .gitignore already lists this file, and it is created anew on every machine.

Thanks for reading!

Donate to the site — we work hard for you.