3

I have a setting in my APP/Config/config.php I have a line that defines the host:

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

Whenever I run the cake console, I get the error:

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

Why is this appearing and how can I solve this problem?

mgPePe
  • 5,677
  • 12
  • 52
  • 85

2 Answers2

4

Its because you are running from console, (cakePHP uses PHP-CLI)

$_SERVER['HTTP_HOST']

its only filled when running from a browser (web server)

Braian
  • 93
  • 7
2

@Braian is right.

You could do the following in your config file:

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

Alternatively, you could query your database for a host setting of some sort - if your hosts are different based on the environment - and use Configure::write to set your config value before your console scripts main logic is run.