marcovtwout / yii-environment
Environment class for Yii, used to set configuration for console and web apps depending on the server environment.
Installs: 184 289
Dependents: 0
Suggesters: 0
Security: 0
Stars: 29
Watchers: 8
Forks: 11
Open Issues: 0
Requires
- php: >=5.3
This package is auto-updated.
Last update: 2024-10-26 00:47:24 UTC
README
Environment-based configuration class (for example in development, testing, staging and production).
Simple class used to set configuration and debugging depending on environment. Using this you can predefine configurations for use in different environments, like development, testing, staging and production.
The main config (main.php
) is extended to include the Yii paths and debug flags.
There are mode_<environment>.php
files for overriding and extending main.php for specific environments.
Additionally, you can overrride the resulting config by using a local.php
config, to make
changes that will only apply to your specific installation.
This class was designed to have minimal impact on the default Yii generated files. Minimal changes to the index/bootstrap and existing config files are needed.
The Environment is determined with PHP's getenv(), which searches $_SERVER and $_ENV. There are multiple ways to set the environment depending on your preference. Setting the environment variable is trivial on both Windows and Linux, instructions included. You can optionally override the environment on a per-project basis.
If you want to customize this class or its config and modes, extend it! (see ExampleEnvironment.php)
Requirements
Yii 1.1.*
Installation
Installation via Composer
Since version 5 the preferred installation method is through Composer.
We assume the vendor directory is placed in protected/vendor
.
-
Add the dependency to your project
php composer.phar require marcovtwout/yii-environment "5.*"
-
Modify your
index.php
(and other bootstrap files) -
Modify your
main.php
config file and add mode specific config files -
Set your local environment (see next section)
Installation from zip file
See the previous instructions: https://github.com/marcovtwout/yii-environment/tree/4.0.1#installation
Setting environment
Here are some examples for setting your environment to DEVELOPMENT
.
Windows
- Go to: Control Panel > System > Advanced > Environment Variables
- Add new SYSTEM variable: name =
YII_ENVIRONMENT
, value =DEVELOPMENT
Linux/Mac
- Open profile files:
- Global bash shell:
/etc/profile
- Apache (as service):
/etc/apache2/envvars
- Add the following line:
export YII_ENVIRONMENT="DEVELOPMENT"
Apache only (cannot be used for console applications)
- Check if mod_env is enabled
- Open your
httpd.conf
or create a.htaccess
file - Add the following line:
SetEnv YII_ENVIRONMENT DEVELOPMENT
Project only
- Create a file
mode.php
in the config directory of your application. - Set the content of the file to:
DEVELOPMENT
Usage
Update bootstrap files
Override environment
You can override the automatic determination of the current environment if needed:
- Pass the environment to the constructor:
new Environment('TEST')
- For console applications, you can also do:
YII_ENVIRONMENT=STAGING ./yiic
Structure of config directory
Your protected/config/
directory will look like this:
config/main.php (Global configuration)
config/mode_development.php (Environment-specific configurations)
config/mode_test.php
config/mode_staging.php
config/mode_production.php
config/local.php (Optional, local override for mode-specific config. Don't put in your SVN!)
Modify your config/main.php
Optional: in configConsole you can copy settings from configWeb by using value key inherit
(see examples folder).
Create mode-specific config files
Create config/mode_<mode>.php
files for the different modes. These will override or merge attributes that exist in the main config.
- See example-config/mode_development.php
- See example-config/mode_test.php
- See example-config/mode_staging.php
- See example-config/mode_production.php
Optional: also create a config/local.php
file for local overrides.
Credits
Original sources: http://www.yiiframework.com/doc/cookbook/73/