Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

createAbsoluteUrl does not work in console app

Tags:

yii2

In Console app createAbsoluteUrl does not work. Do I need to change some setting in config file to made it work.

As per my understanding the request component is not available in Console app. So where do I specify the base URL.

I did not find anything on this. Any help would be appreciated. Thank you.

like image 734
Chinmay Waghmare Avatar asked Dec 19 '22 23:12

Chinmay Waghmare


1 Answers

You are unable to create URL in console application while you will receive the following error:

'Please configure UrlManager::scriptUrl correctly as you are running a console application.'

To get rid of this message and work with UrlManager, in your console.php config file, add the following configuration:

'urlManager' => [
        'class' => 'yii\web\UrlManager',
        'scriptUrl' => 'http://path/to'
]

Now, if you write the following lines in your console app:

echo \Yii::$app->getUrlManager()->createAbsoluteUrl('test/test');
echo "\n";
echo \yii\helpers\Url::toRoute('test/test');

You will see the following output:

http://path/to?r=test%2Ftest
http://path/to?r=test%2Ftest
like image 94
Ali MasudianPour Avatar answered Mar 11 '23 10:03

Ali MasudianPour