11

Slim 4 - Configuration

 3 years ago
source link: https://odan.github.io/2021/03/21/slim4-laminas-config.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Slim 4 - Configuration | Daniel Opitz - Blog

Daniel Opitz - Blog

Developer, Trainer, Open Source Contributor

Blog About me Donate

Slim 4 - Configuration

Daniel Opitz

Daniel Opitz

21 Mar 2021

Table of contents

Requirements

Introduction

Configuration files allow you to configure things like your database connection information, your mail server information, as well as various other core configuration values such as your application timezone and encryption key.

Installation

The laminas/laminas-config component is designed to simplify access to configuration data within applications. It provides a nested object, property-based user interface for accessing this configuration data within application code. The configuration data may come from a variety of formats supporting hierarchical data storage.

composer require laminas/laminas-config

Container Setup

PHP-based configuration files are often recommended due to the speed with which they are parsed, and the fact that they can be cached by opcode caches.

Configuration data are made accessible to the Laminas\Config\Config constructor via an associative array, which may be multi-dimensional so data can be organized from general to specific.

The following code illustrates how to use PHP configuration files.

Add a DI container definition for Laminas\Config\Config:class in config/container.php.

<?php

use Laminas\Config\Config;
// ...

return [
    // ...
    
    Config::class => function () {
        return new Config(require __DIR__ . '/settings.php');
    },
];

Configuration

All the configuration files will be stored in the config/ directory.

If not exists, create a file config/settings.php.

The settings file must return an associative array with multi-dimensional data.

Example:

$settings = [];

$settings['upload_directory'] = __DIR__ . '/../uploads';

$settings['logger'] = [
    'name' => 'app',
    'path' => __DIR__ . '/../logs',
    'filename' => 'app.log',
    'level' => \Monolog\Logger::DEBUG,
    'file_permission' => 0775,
];

$settings['db'] = [
    // ...
];

// ...

return $settings;

Usage

Container Settings

To configure other container entries within the DI container you can access the Config object like this:

// Get Config object
$config = $container->get(Config::class);

// Read config value
$loggerSettings = $config->get('logger');

If you still have the DI container “settings” entry from the previous Slim tutorial, you can refactor the settings array and remove it from the DI container.

LoggerFactory::class => function (ContainerInterface $container) {
    return new LoggerFactory($container->get('settings')['logger']);
},
LoggerFactory::class => function (ContainerInterface $container) {
    return new LoggerFactory($container->get(Config::class)->get('logger'));
},

Remove this entry, if exists:

'settings' => function () {
    return require __DIR__ . '/settings.php';
},

Accessing Configuration Values

To access the Config object, we must first declare it in the constructor so that it can be automatically injected by the DI Container.

Example:

<?php

namespace App\Action;

use Laminas\Config\Config;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

final class ExampleAction
{
    private Config $config;

    public function __construct(Config $config)
    {
        $this->config = $config;
    }

    public function __invoke(
        ServerRequestInterface $request,
        ResponseInterface $response
    ): ResponseInterface {
        $uploadDirectory = $this->config->get('upload_directory');
        // ...
        
        return $response;
    }
}

Accessing Configuration Values

You may easily access your configuration values using the config property within your class.

$value = $this->config->get('timezone');

// Retrieve a default value if the configuration value does not exist...
$value = $this->config->get('timezone', 'Europe/Berlin');

Conclusion

As you can see, it is now possible to inject the Config object where you want to read configuration values. The use of an object also has the advantage that the configuration can now be injected automatically via autowiring.

Read more

© 2021 Daniel Opitz | Twitter


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK