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:
- bootstrap.php - “Prestart” settings for the environment.
- bootstrap.local.php - “Prestart” settings for the local server.
- defaults.php - Environment default settings.
- defaults.local.php - The default settings of the local server.
- defaults.local.example - A sample of settings so that you can quickly create local server settings.
- console.php - Environment console settings.
- web.php - Settings for the web application environment.
- .gitignore - Rules for ignoring local settings when working with git repository.
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
The file
/config/bootstrap.php is responsible for the “pre-launch” environment settings.
<?php
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, ], ], ];
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:
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.