📜 ⬆️ ⬇️

My view on yii 2 configuration files

I decided to share a streamlined and convenient configuration file for me (configuration / folder), which I use in all my projects on yii 2 basic.

The essence


I want to demonstrate my idea by the example of the / config folder of one of my current projects:

As can be seen from the file structure, the settings are divided into local and environment settings. Environment settings usually act as battle server settings, since it is the battle server that is the environment: the developers connect to it, it is responsible for the site output, and so on.

Description


Connection of settings begins with the file /web/index.php , in which the necessary configurations are connected and merged into one:

<?php // defined('YII_DEBUG') or define('YII_DEBUG', true); // defined('YII_ENV') or define('YII_ENV', 'dev'); require(__DIR__ . '/../vendor/autoload.php'); require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php'); require __DIR__ . '/../config/bootstrap.php'; //    $config = \yii\helpers\ArrayHelper::merge( require __DIR__ . '/../config/defaults.php', //      require __DIR__ . '/../config/web.php' //      ); (new yii\web\Application($config))->run(); 

The file /config/bootstrap.php is responsible for the “pre-launch” environment settings.
 <?php //    $path_local = __DIR__ . '/bootstrap.local.php'; if(file_exists($path_local)) { require $path_local; } 

As can be seen from the code section, there is usually no useful information in the file, but it contains the code responsible for checking and connecting local files. This approach does not worry about the presence of these files.
')
The /config/bootstrap.local.php file is responsible for the “pre-launch” settings for the local server:

 <?php error_reporting(E_ALL); ini_set('display_errors', 1); 

The file /config/defaults.php is responsible for the default settings of the environment and usually contains the settings for connecting to the database and other global settings for the application (some settings were removed from the file for clarity):

 <?php $defaults = [ 'basePath' => dirname(__DIR__), 'timeZone' => 'UTC', 'components' => [ 'db' => [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=webulla', 'username' => 'webulla', 'password' => 'password', 'charset' => 'utf8', 'tablePrefix' => '', 'enableSchemaCache' => true, 'enableQueryCache' => true, ], ], ]; //    $path_local = __DIR__ . '/defaults.local.php'; if(file_exists($path_local)) { $defaults = \yii\helpers\ArrayHelper::merge($defaults, require $path_local); } return $defaults; 

The /config/defaults.local.php file is responsible for the default settings of the local server and overrides the database connection settings:

 <?php return [ 'components' => [ 'db' => [ 'dsn' => 'mysql:host=localhost;dbname=webulla', 'username' => 'root', 'password' => '', ] ], ]; 

Using the files /config/defaults.php and /config/defaults.local.php as an example, the configuration files for the console and the web environment are configured.

The /config/.gitignore file responds so that local settings do not spill onto the server:

 # ignore local configs *.local.php 

Conclusion


This approach allows me to easily and quickly edit the settings on the local and combat server. When using the git repository, this makes life even easier: you don’t need to worry about any local settings going to the server.

Source: https://habr.com/ru/post/264969/


All Articles