How to build URL in console application?

Hello everyone,

I tried to build a URL in a console application like this:

$url = Yii::app()->getUrlManager()->createUrl(‘route/name’, $_GET_PARAMS);

but "echo $url" I got:

./route/name/xxxxx

the "http://hostname" was missed.

How can I build a full url in console?

Many thanks!

Hi, welcome to forum.

One solution is to use Parameterizing Hostnames (at the bottom). When you then create an url, it will contain the full host info. A problem is that the config is a little bloated and when you change your domain, you must change all url rules. You may do something like this for flexibility:





// Use different hosts for debugging and production + easy to modify later

$host = YII_DEBUG ? 'http://localhost' : 'http://example.com';


'components' => array(


   ...


   'urlManager' => array(

      ...

      'rules' => array(

         "{$host}" => 'site/index',

         "{$host}/contact" => 'site/contact',

         ...

      ),

   ),


   ...


),



Another solution is to just add the correct host:




$url = Yii::app()->params['host'] . Yii::app()->request->createUrl('site/index');



You can define the params array in config. If you want to automate this, you may create a special UrlManager component for the console application that will auto-prefix a created url with the correct host.