您不需要为每个环境复制您的应用程序。 Codeigniter 可以处理多种配置。您只需为每个环境设置一个文件夹。
让我们假设以下组织:
Root/
-Application
-System
-Assets
-css
-js
-images
-index.php
我们将有 3 个环境:开发/测试/生产。
我们知道我们的应用程序将根据环境具有不同的 base_url 和数据库配置。这意味着每次我们将应用程序从一个环境移动到另一个环境时,我们都需要修改 config.php 和 database.php。
CodeIgniter 为我们提供了一种简单的方法:
http://www.codeigniter.com/user_guide/general/environments.html
http://www.codeigniter.com/user_guide/libraries/config.html#environments
我们只需要在 application/config 中设置 3 个文件夹
-application
-config
- development
- testing
- production
在这些新文件夹中,只需放置在每个环境中都不同的配置文件。在我们的例子中,config.php 和 database.php
-application
-config
- development
-config.php
-database.php
- testing
-config.php
-database.php
- production
-config.php
-database.php
- autoload.php
- ...
最后,在根 index.php 中只需要修改以下行来告诉 CI 它是在哪个环境中:
define('ENVIRONMENT', 'development'); //for dev env
define('ENVIRONMENT', 'testing'); //for testing env
define('ENVIRONMENT', 'production'); //for prod env
开发/测试/生产是 CI 中的默认环境,但您可以通过在 application/config 中创建文件夹并修改根 index.php 中的开关来创建自己的环境:
switch (ENVIRONMENT)
{
case 'development':
error_reporting(E_ALL);
break;
case 'server':
error_reporting(E_ALL);
break;
case 'testing':
case 'production':
error_reporting(0);
break;
/*CUSTOM ENV :*/
case 'custom_env':
error_reporting(E_ALL);
break;
default:
exit('The application environment is not set correctly.');
}