, (*1)
Environment Plugin for CakePHP
for CakePHP 2.x, (*2)
Requirement
- PHP version: PHP 5.2+
- CakePHP version: 2.x Stable
Installation
- Clone/Copy the files in this diectory into
app/Plugin/Environment
- Ensure the plugin is loaded in
app/Config/bootstrap.php
by calling CakePlugin::load('Environment', array('bootstrap' => true))
Using Composer
Add the plugin to your project's composer.json
- something like this:, (*3)
{
"require": {
"quest/cakephp-environment": "master"
}
}
Because this plugin has the type cakephp-plugin
set in its own composer.json
, Composer will install it inside your /Plugins
directory, rather than in the usual vendors file. It is recommended that you add /Plugins/Environment
to your .gitignore file. (Why? read this.), (*4)
Manual
GIT Submodule
In your app directory type:, (*5)
git submodule add -b master git://github.com/quest/cakephp-environment.git Plugin/Environment
git submodule init
git submodule update
GIT Clone
In your Plugin
directory type:, (*6)
git clone -b master git://github.com/quest/cakephp-environment.git Environment
Enable plugin
In 2.0 you need to enable the plugin in your app/Config/bootstrap.php
file:, (*7)
CakePlugin::load('Environment', array('bootstrap' => true));
Usage
Create file app/Config/env.php
with this example content., (*8)
/**
* Domains environments
* IMPORTANT: This lines on the top of the file
*/
Configure::write('Environment.domains', array(
'development' => '.*',
'production' => '^(.+\.)?mysite\.com$'
));
/**
* Development settings
*/
Environment::write(array(
'debug' => 2,
'Security.salt' => '6978hjkhKjkhskjhd698KGNSLdsDLsdKSAsdf8778sdfg',
'Security.cipherSeed' => '57283694289374902834892039823756894'
), 'development');
/**
* Production settings
*/
Environment::write(array(
'debug' => 0
), 'production');
Environment Detection
In order to detect the project environment and apply their settings, use the application hostname, just like this:, (*9)
Configure::write('Environment.domains', array(
'development' => '^myapp\.local$',
'production' => '^mycapp\.com$'
));
You must use regex to define the hostname. You can create many environments as you want., (*10)
Methods
Environment::get()
Get the current environment depending on hostname requested:, (*11)
echo Environment::get(); // print 'development' text
Environment::set(string $environment)
Avoid set the environment based on hostname request, by using set()
method. It will overwrite any other environment previously settled., (*12)
Environment::set('testing'); // true
Environment::is(string $environment)
Use this method to check the current/working environment., (*13)
if (Environment::is('development')) {
echo 'this is development';
}
else {
echo 'this is ' . Environment::get();
}
Environment::write()
You can write environment settings by using the write()
method in two ways:, (*14)
Multiple, (*15)
Environment::write(array(
'debug' => 2,
'database' => array(
'login' => 'root',
'host' => 'localhost',
'password' => '',
)
), 'development');
Single, (*16)
Environment::write('debug', 0, 'production');
Environment::read()
Read environment settings by using the read()
method:, (*17)
- $key: Key to find
- $environment: Optional â Environment scope
// databases.php
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => Environment::read('database.host'),
'login' => Environment::read('database.login'),
'password' => Environment::read('database.password'),
'database' => 'database_name',
'prefix' => '',
'encoding' => 'utf8',
);
}
Environment::write()
also can be used to update or modify Cake's app/Config/core.php
file settings. For example:, (*18)
/**
* Development settings
*/
Environment::write(array(
'debug' => 2,
'Security.salt' => '6978hjkhKjkhskjhd698KGNSLdsDLsdKSAsdf8778sdfg',
'Security.cipherSeed' => '57283694289374902834892039823756894'
), 'development');
/**
* Production settings
*/
Environment::write(array(
'debug' => 0,
'Security.salt' => '6978hjkhKjkhskjhd698KGNSLdsDLsdKSAsdf8778sdfg',
'Security.cipherSeed' => '57283694289374902834892039823756894'
), 'production');
TODO
Support
To report bugs or request features, please visit the Issue Tracker., (*19)
Contributing to this Plugin
Please feel free to contribute to the plugin with new issues, requests, unit tests and code fixes or new features. If you want to contribute some code, create a feature branch from develop, and send us your pull request. Unit tests for new features and issues detected are mandatory to keep quality high., (*20)
License
Copyright 2014, Victor San MartĂn, (*21)
Licensed under The MIT Licensebr/
Redistributions of files must retain the above copyright notice., (*22)