Consolidating Configuration Over Yii2

A little while back I was engaged into a project that centers around Yii2. Though it has one problem. Configuration. Yep. The configuration for Yii2 is a total mess.

To give you a better perspective on how it works, each module has its own configuration folder and a common configuration folder. Lastly each having their own web or www/public folder.

To wire all of them together it does require_once or include throughout the header of a config file. Adding to the mess is production config values are included in the SCM! To remedy this I have to do three things

  • Centralize the configuration
  • Config values utilize environment variables from an .env file
  • Centralize the public folder

Other Problems

Throughout the whole ordeal I encountered certain problems that gave me an initial perspective on how Yii2 works in the background. I have to do some quirks in order to go around these problems and make the site work.

  • base_path is based on the module base directory. It created a problem whereas aliases for assets, views, and config files will resolve to the module's base path instead of the newly altered structure.
  • request, errorHandler components when included in the console config creates an error in the default yii console runner.


The two most significant changes on the Yii project is the addition of a config/, public/ folders with their respective files and a console runner that can run the changes made throughout the structure.

One of the biggest advantages in the new structure is you don't have to worry about same values in your configs throughout in your project/module directories. Lastly it provides a reasonable improvement in leaving out sensitive values out from the SCM.

Source Code

The code speaks for itself. If you have questions please contact me over Twitter.

Show Comments