Get base URL in Yii console application

Harish Anchu picture Harish Anchu · Dec 19, 2012 · Viewed 14.1k times · Source

How to get base URL in a Yii CConsoleApplication application?

I tried Yii::app()->request->getBaseUrl(true) and ended up with the following error.

Undefined index: SERVER_NAME (/var/www/yii/framework/web/CHttpRequest.php:279)

Answer

Asgaroth picture Asgaroth · Dec 19, 2012

There is no request object in a console application. the request object in a web application its an instance of CHttpRequest, if you are generating URLs in an offline task, you have to configure the baseUrl in some other way, perhaps in the configuration:

"request" => array(
    'hostInfo' => 'http://localhost',
    'baseUrl' => '/yii-project/index-test.php',
),

// OR

'request' => array(
    'hostInfo' => 'http://localhost',
    'baseUrl' => '/yii-project',
    'scriptUrl' => 'index-test.php',
),