我的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
为什么会出现这种情况?如何解决这个问题呢?
答案 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设置您的配置值。