Chinmay Waghmare
Chinmay Waghmare

Reputation: 5456

createAbsoluteUrl does not work in console app

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.

Upvotes: 4

Views: 8980

Answers (1)

Ali MasudianPour
Ali MasudianPour

Reputation: 14459

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

Upvotes: 12

Related Questions