CakePHP控制台出错“未定义索引:HTTP_HOST”

时间:2013-09-13 16:08:27

标签: cakephp-2.0 settings cake-console

我的APP/Config/config.php中有一个设置我有一行定义主机:

define('SITE_URL','http://'.$_SERVER['HTTP_HOST']);

每当我运行蛋糕控制台时,我都会收到错误:

Notice (8): Undefined index: HTTP_HOST [APP/Config/config.php, line 144]
Code Context
include - APP/Config/config.php, line 144
PhpReader::read() - CORE/Cake/Configure/PhpReader.php, line 80
Configure::load() - CORE/Cake/Core/Configure.php, line 267
include - APP/Config/bootstrap.php, line 190
Configure::bootstrap() - CORE/Cake/Core/Configure.php, line 92
require - CORE/Cake/bootstrap.php, line 146
ShellDispatcher::_bootstrap() - CORE/Cake/Console/ShellDispatcher.php, line 131
ShellDispatcher::_initEnvironment() - CORE/Cake/Console/ShellDispatcher.php, line 101
ShellDispatcher::__construct() - CORE/Cake/Console/ShellDispatcher.php, line 57
ShellDispatcher::run() - CORE/Cake/Console/ShellDispatcher.php, line 68
[main] - APP/Console/cake.php, line 33

为什么会出现这种情况?如何解决这个问题呢?

2 个答案:

答案 0 :(得分:3)

因为你是从控制台运行的,(cakePHP使用PHP-CLI)

$_SERVER['HTTP_HOST']

仅在从浏览器(Web服务器)

运行时填充

答案 1 :(得分:1)

@Braian是对的。

您可以在配置文件中执行以下操作:

if(php_sapi_name() === 'cli') { 
    define('SITE_URL','http://{DEFAULT HOST HERE};
} else {
    define('SITE_URL','http://'.$_SERVER['HTTP_HOST']);
}

或者,您可以在数据库中查询某种类型的主机设置 - 如果您的主机根据环境而不同 - 并在运行控制台脚本主逻辑之前使用Configure :: write设置您的配置值。