What is the best way to use general config file

Hello everyone

Here we work hard to move all of the customer services to MVC v3 and we are working on to man modules, I hope some of the modules can be published as free software before the end of the year.

What is the best method for loading the general config file on all modules, for example, I need these configs:

  • General information ( like application name and , )
  • Some config for user module ( for example secret key for JWT )
  • Some config for notification module ( for example SMTP config and SMS config )
  • Some config for media module ( Image size and upload path )
    and…

I want to put all of the configs in a single file or different files on the laminas config folder and can access the config file in all of the module files ( by using factory or another way )

What is tye your idea? what is the best way

The ‘config’ service already includes the merged configuration from all modules and loaded config files.

1 Like

Thank you very much, I check it, just question
which one is better ( performance and cache )?

$config = new Config(include 'config.php');
$config = $config->toArray();

or

$config = Factory::fromFile('config.php');

I have just a problem whit it, I can call it in all of my files and each file dir is different for each load I have to set a new path, does laminas have a global path setting? then all paths can be the same

Fetch the service and inject it in your services instead. In a factory:

$config = $container->get('config');
return new MyService($config['some_setting'], $config['some_other_setting']);

The config service is already:

  • loaded efficiently
  • cached (in production mode)
  • preserved in memory (no double loading)
2 Likes

Dear @voltan,

Laminas MVC provides an autoload configuration method which helps a lot and will help maintain the project as well. The Laminas MVC configuration gives a general approach which says, you can override any configuration by naming them as *.global.php or *.local.php. For example:

<?php
//config/autoload/global.php sample content

return [
    'db' => [
        'driver' => 'Pdo',
        'dsn'    => 'mysql:dbname=somedatabase;host=localhost;charset=utf8',
    ],
    'smtp_config' => [
        'port' => 365,
        'host' => 'somehost',
        'username' => 'some user name',
        'password' => 'some password',
    ],
];

To override the above configuration create a local.php file in the Laminas MVC application.

<?php
//config/autoload/local.php sample content

return [
    'db' => [
        'driver' => 'Pdo',
        'dsn'    => 'mysql:dbname=LocalDatabase;host=localhost;charset=utf8',
        'username' => 'root',
        'password' => 'password',
    ],
    'smtp_config' => [
        'port' => 1015,
        'host' => 'localhost',
        'username' => 'local user name',
        'password' => 'local password',
    ],
];

I hope it helps. Thanks!

1 Like