summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--index.php48
-rw-r--r--system/core/Common.php17
-rw-r--r--system/core/Config.php22
-rw-r--r--system/core/Loader.php15
-rw-r--r--system/database/DB.php16
-rw-r--r--user_guide/database/configuration.html4
-rw-r--r--user_guide/libraries/config.html17
7 files changed, 110 insertions, 29 deletions
diff --git a/index.php b/index.php
index 5bb53d2f2..918c80259 100644
--- a/index.php
+++ b/index.php
@@ -2,15 +2,46 @@
/*
*---------------------------------------------------------------
- * PHP ERROR REPORTING LEVEL
+ * APPLICATION ENVIRONMENT
*---------------------------------------------------------------
*
- * By default CI runs with error reporting set to ALL. For security
- * reasons you are encouraged to change this to 0 when your site goes live.
- * For more info visit: http://www.php.net/error_reporting
+ * You can load different configurations depending on your
+ * current environment. Setting the environment also influences
+ * things like logging and error reporting.
*
+ * This can be set to anything, but default usage is:
+ *
+ * development
+ * testing
+ * production
+ *
+ * NOTE: If you change these, also change the error_reporting() code below
+ *
+ */
+ define('ENVIRONMENT', 'development');
+/*
+ *---------------------------------------------------------------
+ * ERROR REPORTING
+ *---------------------------------------------------------------
+ *
+ * Different environments will require different levels of error reporting.
+ * By default development will show errors but testing and live will hide them.
*/
- error_reporting(E_ALL);
+
+ switch (ENVIRONMENT)
+ {
+ case 'development':
+ error_reporting(E_ALL);
+ break;
+
+ case 'testing':
+ case 'production':
+ error_reporting(0);
+ break;
+
+ default:
+ exit('The application environment is not set correctly.');
+ }
/*
*---------------------------------------------------------------
@@ -22,7 +53,7 @@
* as this file.
*
*/
- $system_path = "system";
+ $system_path = 'system';
/*
*---------------------------------------------------------------
@@ -38,7 +69,7 @@
* NO TRAILING SLASH!
*
*/
- $application_folder = "application";
+ $application_folder = 'application';
/*
* --------------------------------------------------------------------
@@ -94,9 +125,6 @@
// END OF USER CONFIGURABLE SETTINGS. DO NOT EDIT BELOW THIS LINE
// --------------------------------------------------------------------
-
-
-
/*
* ---------------------------------------------------------------
* Resolve the system path for increased reliability
diff --git a/system/core/Common.php b/system/core/Common.php
index b5adfacb3..cd6b93355 100644
--- a/system/core/Common.php
+++ b/system/core/Common.php
@@ -208,15 +208,20 @@
return $_config[0];
}
+ $file_path = APPPATH.'config/'.ENVIRONMENT.'/config'.EXT;
+
// Fetch the config file
- if ( ! file_exists(APPPATH.'config/config'.EXT))
- {
- exit('The configuration file does not exist.');
- }
- else
+ if ( ! file_exists($file_path))
{
- require(APPPATH.'config/config'.EXT);
+ $file_path = APPPATH.'config/config'.EXT;
+
+ if ( ! file_exists($file_path))
+ {
+ exit('The configuration file does not exist.');
+ }
}
+
+ require($file_path);
// Does the $config array exist in the file?
if ( ! isset($config) OR ! is_array($config))
diff --git a/system/core/Config.php b/system/core/Config.php
index bfb60fa44..da22222dc 100644
--- a/system/core/Config.php
+++ b/system/core/Config.php
@@ -74,6 +74,8 @@ class CI_Config {
*
* @access public
* @param string the config file name
+ * @param boolean if configuration values should be loaded into their own section
+ * @param boolean true if errors should just return false, false if an error message should be displayed
* @return boolean if the file was loaded correctly
*/
function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
@@ -82,8 +84,8 @@ class CI_Config {
$loaded = FALSE;
foreach($this->_config_paths as $path)
- {
- $file_path = $path.'config/'.$file.EXT;
+ {
+ $file_path = $path.'config/'.ENVIRONMENT.'/'.$file.EXT;
if (in_array($file_path, $this->is_loaded, TRUE))
{
@@ -91,11 +93,17 @@ class CI_Config {
continue;
}
- if ( ! file_exists($path.'config/'.$file.EXT))
+ if ( ! file_exists($file_path))
{
- continue;
+ log_message('debug', 'Config for '.ENVIRONMENT.' environment is not found. Trying global config.');
+ $file_path = $path.'config/'.$file.EXT;
+
+ if ( ! file_exists($file_path))
+ {
+ continue;
+ }
}
-
+
include($file_path);
if ( ! isset($config) OR ! is_array($config))
@@ -136,9 +144,9 @@ class CI_Config {
{
return FALSE;
}
- show_error('The configuration file '.$file.EXT.' does not exist.');
+ show_error('The configuration file '.ENVIRONMENT.'/'.$file.EXT.' and '.$file.EXT.' do not exist.');
}
-
+
return TRUE;
}
diff --git a/system/core/Loader.php b/system/core/Loader.php
index 07166188e..ca2f016e7 100644
--- a/system/core/Loader.php
+++ b/system/core/Loader.php
@@ -880,8 +880,19 @@ class CI_Loader {
foreach ($config_component->_config_paths as $path)
{
// We test for both uppercase and lowercase, for servers that
- // are case-sensitive with regard to file names
- if (file_exists($path .'config/'.strtolower($class).EXT))
+ // are case-sensitive with regard to file names. Check for environment
+ // first, global next
+ if (file_exists($path .'config/'.ENVIRONMENT.'/'.strtolower($class).EXT))
+ {
+ include_once($path .'config/'.ENVIRONMENT.'/'.strtolower($class).EXT);
+ break;
+ }
+ elseif (file_exists($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).EXT))
+ {
+ include_once($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).EXT);
+ break;
+ }
+ elseif (file_exists($path .'config/'.strtolower($class).EXT))
{
include_once($path .'config/'.strtolower($class).EXT);
break;
diff --git a/system/database/DB.php b/system/database/DB.php
index fb0516ba4..513e5aefd 100644
--- a/system/database/DB.php
+++ b/system/database/DB.php
@@ -27,7 +27,21 @@ function &DB($params = '', $active_record_override = NULL)
// Load the DB config file if a DSN string wasn't passed
if (is_string($params) AND strpos($params, '://') === FALSE)
{
- include(APPPATH.'config/database'.EXT);
+
+ $file_path = APPPATH.'config/'.ENVIRONMENT.'/database'.EXT;
+
+ if ( ! file_exists($file_path))
+ {
+ log_message('debug', 'Database config for '.ENVIRONMENT.' environment is not found. Trying global config.');
+ $file_path = APPPATH.'config/database'.EXT;
+
+ if ( ! file_exists($file_path))
+ {
+ continue;
+ }
+ }
+
+ include($file_path);
if ( ! isset($db) OR count($db) == 0)
{
diff --git a/user_guide/database/configuration.html b/user_guide/database/configuration.html
index 8e6fe1f42..e9db5fc87 100644
--- a/user_guide/database/configuration.html
+++ b/user_guide/database/configuration.html
@@ -61,9 +61,7 @@ Configuration
<h1>Database Configuration</h1>
<p>CodeIgniter has a config file that lets you store your database connection values (username, password, database name, etc.).
-The config file is located at:</p>
-
-<p><kbd>application/config/database.php</kbd></p>
+The config file is located at <samp>application/config/database.php</samp>. You can also set database connection values for specific <a href="../libraries/config.html">environments</a> by placing <strong>database.php</strong> it the respective environment config folder.</p>
<p>The config settings are stored in a multi-dimensional array with this prototype:</p>
diff --git a/user_guide/libraries/config.html b/user_guide/libraries/config.html
index 6b48b2515..98b6052a9 100644
--- a/user_guide/libraries/config.html
+++ b/user_guide/libraries/config.html
@@ -150,6 +150,23 @@ $site_name = $blog_config['site_name'];</code>
<p>Where <var>item_name</var> is the $config array index you want to change, and <var>item_value</var> is its value.</p>
+<h2>Environments</h2>
+
+<p>You can set the environment of you application and load config items depending on the current environment. It also disables PHP from displaying errors in environments other than development. To set your environment, open <strong>index.php</strong>, located at the root and change the <var>ENVIRONMENT</var> constant. By default, there is support for a development, test and production environment.</p>
+
+<code>
+define('<var>ENVIRONMENT</var>', '<var>development</var>');
+</code>
+
+<p>To make a config file environment-aware, copy the file from <samp>application/config/</samp> to <samp>application/config/development/</samp>, depending on the environment the config file belongs to. You can place the following configuration files in environment folders:</p>
+
+<ul>
+<li>Default config files</li>
+<li>Database config files</li>
+<li>Custom config files</li>
+</ul>
+
+<p><strong>Note:</strong> CodeIgniter always tries to load the config file for the current environment first. If the file does not exist, the global config file (i.e. <samp>application/config/</samp>) is loaded. This means you are not obligated to place <strong>all</strong> your config files (but rather the files that change per environment) in an environment folder.</p>
<h2>Helper Functions</h2>