Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default main.php config file for all projects in Yii2

I am developing multiple websites based on Yii2 framework and I was wondering if there is any good way or if it's even good idea or not to create a shared basic config main.php file.

The reason behind this is that all my projects are based on same system, have a lot of similarities and I simply want to keep things DRY. Configs are also mostly same - the only differences actually are database names and few other values. Even urlManager rules are not so different.

The problem is that when for example I would want to add additional FileTarget to logger targets for all websites I have to do this for each project separately and I can never be sure if all project configs are up to date and have everything set up properly.

So I was wondering if there is any Yii way of creating default config file shared in multiple projects or if it's simply a bad idea. For example I could create bootstrap component which sets defaults or add default config as first item in the list of merged configs in frontend\web\index.php file. I have private packages added in composer so it's not a problem adding file that will be shared in all projects.

like image 635
labm0nkey Avatar asked Aug 25 '16 12:08

labm0nkey


People also ask

What is yii2 basic project template?

Yii 2 Basic Project Template is a skeleton Yii 2 application best for rapidly creating small projects. The template contains the basic features including user login/logout and a contact page. It includes all commonly used configurations that would allow you to focus on adding new features to your ...

How do I set default configurations for a class in Yii?

It allows you to specify a set of the so-called default configurations which will be applied to ALL instances of the specified classes when they are being created using Yii::createObject (). The default configurations can be specified by calling Yii::$container->set () in the bootstrapping code.

How to create and initialize a database connection in Yii?

In the following, a configuration is used to create and initialize a database connection: The Yii::createObject () method takes a configuration array as its argument, and creates an object by instantiating the class named in the configuration.

What is createobject() method in Yii?

The Yii::createObject () method is implemented based on a dependency injection container . It allows you to specify a set of the so-called default configurations which will be applied to ALL instances of the specified classes when they are being created using Yii::createObject ().


2 Answers

I am working on the same question for a pretty long time and tried several ideas.

At the moment I've developed nice solution for the issues you describe. The heart of the solution is hiqdev/composer-config-plugin - composer plugin which merges configs from extensions.

For example we have extension that is base for all our web projects hiqdev/hisite-core it contains basic Yii application config and params:

return [
    'id' => 'hisite',
    'name' => 'HiSite',
    'aliases' => [
        '@bower' => '@vendor/bower-asset',
        ...

These configs are enabled in composer.json with such lines:

"extra": {
    "config-plugin": {
        "params": "src/config/params.php",
        "hisite": "src/config/hisite.php"
    }
},

And that's it. You require hiqdev/hisite-core and after running composer update you have assembled configs (you may have multiple config) in vendor/hiqdev/config/hisite.php

Then you use it from your index.php like this:

$config = require dirname(dirname(__DIR__)) . '/hiqdev/config/hisite.php';

You can achieve similar behaviour with Yii extension bootstrapping but bootstrap have to run on every page request.

The beauty of the solution is that you can have lots of components every one of them providing own part of config and it works without slowing page rendering time because the config is merged once on composer install/update. (And you can force merging of config with composer dump-autoload, don't forget to run it after changing config).

Using this technique we've creating auto plugging extensions: themes, modules and more that are not required to be configured in main config - you just require them in composer.json and you have it working.

This solution is still work in progress and it's going to evolve and change but we use it for really big Yii2 based application with 30+ extensions and it is already tested and stable enough.

like image 95
hiqsol Avatar answered Dec 25 '22 10:12

hiqsol


As a simpler alternative you can just put a file somewhere and include it from all the configs i.e.

$commonConfig = require '/var/www/common/config.php';
$appConfig = require 'configs/main.php';
$config = array_merge($commonConfig, $appConfig);
// ...
like image 44
Sam Dark Avatar answered Dec 25 '22 11:12

Sam Dark