Yii Framework Forum: How to build URL in console application? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to build URL in console application? Rate Topic: -----

#1 User is offline   liuw 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 21-December 09

Posted 12 March 2010 - 09:48 AM

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!
0

#2 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 12 March 2010 - 03:57 PM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users