php Codeigniter 环境设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20002917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:47:02  来源:igfitidea点击:

Codeigniter Environment setting

phpcodeigniterdevelopment-environment

提问by Zohaib

Codeigniter development environment is not setting. I always use this code in index.php. but i don't understand why i am getting "production" as output while i am working on localhost.

Codeigniter 开发环境未设置。我总是在 index.php 中使用这段代码。但我不明白为什么我在本地主机上工作时将“生产”作为输出。

switch(dirname(__FILE__)){
 case "H:\wamp\www\sitedirectory":
    define('ENVIRONMENT', 'development');
 break;
 default:
    define('ENVIRONMENT', 'production');
 break;
 }

  echo ENVIRONMENT ;   // output is "production" while i am on localhost
  echo dirname(__FILE__) ;  // output is "H:\wamp\www\sitedirectory"

回答by bmorenate

That's strange. It did the exact same thing for me. Could you try something like this?

真奇怪。它对我做了完全相同的事情。你能试试这样的吗?

switch($_SERVER["HTTP_HOST"]){
 case "localhost":
    define('ENVIRONMENT', 'development');
 break;
 default:
    define('ENVIRONMENT', 'production');
 break;
 }

 echo ENVIRONMENT ;   // output development

回答by Rodrigo Queiroz

To dynamically set the ENVIRONMENT based on the server's IP, below I have used regular expression to check for local IP's such as 127.0.* and 10.0.*.

为了根据服务器的 IP 动态设置环境,下面我使用正则表达式来检查本地 IP,例如 127.0.* 和 10.0.*。

At the root of you project look to index.phpand replace:

在您项目的根目录查找index.php并替换:

define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');

with:

和:

$server_ip = getHostByName(getHostName());

if (preg_match("/^(127\.0\.|10\.0\.).+/i", $server_ip)) {
    define("ENVIRONMENT", "development");
    define("BASEURL", "http://localhost:8000/");
} else {
    define("ENVIRONMENT", "production");
    define("BASEURL", "https://domain.com/");
}

Make sure to replace the value from BASEURLwith your own and in application/config/config.phpadd:

确保BASEURL用您自己的值替换并application/config/config.php添加:

$config['base_url'] = BASEURL;

To improve further add to application/config/database.phpright before the database settings $db['default'] = array(:

为了进一步改进application/config/database.php在数据库设置之前添加$db['default'] = array(

if(ENVIRONMENT !== 'production') {
    $db = [
            'username' => '',
            'password' => '',
            'database' => '',
            'hostname' => '127.0.0.1'
    ];
} else {
    $db = [
            'username' => '',
            'password' => '',
            'database' => '',
            'hostname' => ''
    ];
}

回答by Lucky Soni

Adding on to the other answers. Now the below answer might look like an overkill (if you have to define the environment variables then why use the HTTP_HOST at all? Well in my experience CI fails to reflect any changes made to the environment variables even after restarting apache. It somehow gets the updated values when sending a request from the CLI.)

补充其他答案。现在下面的答案可能看起来有点矫枉过正(如果你必须定义环境变量,那么为什么要使用 HTTP_HOST?根据我的经验,即使在重新启动 apache 之后,CI 也无法反映对环境变量所做的任何更改。它以某种方式获得了从 CLI 发送请求时更新的值。)

if (php_sapi_name() === 'cli') 
{
    // incase the request is made using the cli, the $_SERVER['HTTP_HOST'] will not be set

    define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
} 
else 
{
    switch ($_SERVER["HTTP_HOST"]) 
    {
        case "localhost":
            define('ENVIRONMENT', 'development');
            break;
        default:
            define('ENVIRONMENT', 'production');
            break;
    }
}

回答by Anjani Barnwal

You can Also use this one to auto change Environment in codeigniter

您也可以使用这个来自动更改 codeigniter 中的环境

if ($_SERVER['HTTP_HOST'] == 'localhost'){
    define('ENVIRONMENT', 'development');
}else{
    define('ENVIRONMENT', 'production');
}