Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get base URL in Yii console application

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)

like image 502
Harish Anchu Avatar asked Dec 19 '12 11:12

Harish Anchu


2 Answers

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',
),
like image 160
Asgaroth Avatar answered Oct 02 '22 23:10

Asgaroth


For Yii2 advanced template, create params.php file if it doesn't exist in config directory of your console or common app and paste the following code:

return [
   'frontendUrl' => 'http://yourdomain.com'
];

So that it can be accessed in the following way in console:

echo Yii::$app->params['frontendUrl'];
like image 20
pravindot17 Avatar answered Oct 02 '22 23:10

pravindot17