Get base URL in Yii console application
Asked Answered
R

5

7

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)

Rodeo answered 19/12, 2012 at 11:29 Comment(1)
URL in console? Can you give an example of desired result?Sebastiansebastiano
Y
13

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',
),
Yurt answered 19/12, 2012 at 19:4 Comment(4)
Thanks, Where should I place this?Rodeo
this should go into your console configuration file, in the components section, that is normally protected/config/console.phpYurt
get error on trying to set hostInfo Property "CConsoleApplication.request" is read only.Overlie
@Overlie you should to put in inside "components" array, to into a root config;Seaweed
W
4

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'];
Wrestle answered 10/11, 2016 at 6:43 Comment(0)
O
0

You have to add in config/console.php in components array

'request' => array(
    'hostInfo' => 'http://localhost',
    'baseUrl' => '',
    'scriptUrl' => '',
),

and also add urlManager so it will remove index.php?r=site/action from the url and you will have pretty urls

'urlManager' => array(
    'urlFormat' => 'path',
    'showScriptName' => false,
    'rules' => array(
        '<controller:\w+>' => '<controller>/index',
        '<controller:\w+>/<id:\d+>' => '<controller>/view',
        '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
        '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
    ),
),
Oak answered 26/4, 2017 at 14:25 Comment(0)
K
-3

Please try the following wayt to get the base url page,

echo Yii::app()->getBaseUrl(true);
Kyles answered 19/12, 2012 at 11:39 Comment(5)
How is application base bath in file system related to a base URL?Sebastiansebastiano
It returns base path not base url.Rodeo
This works on Web Application, but not in a Console Application.Rochdale
Please don't put -ve without proper testing , its working codeOrvieto
Great Elby. Can i have your mail id please? i would like to have contact.Kyles
W
-3

get the home url like this:

http://www.demo.local

echo Yii::app()->homeUrl;
Wolter answered 26/9, 2013 at 8:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.