Problem with createUrl and createAbsoluteUrl in console application

For a console command I need to send an email containing (absolute) URLs to the website.

Since Yii::app() has no controller I am creating my own:

$controller=new CController(‘UrlCreator’);


I am getting this error:

Undefined index: SERVER_NAME (C:\htdocs\frameworks\yiiframework\framework\web\CHttpRequest.php:211)

I did not configure the request component in config, but I guess it’s being created to create the URL.

I think my idea of creating absolute URL in console is not right - is there another way to do it?


(Using Yii 1.1.4)

Why not use Yii::app()->createAbsoluteUrl() ?

This is only defined for CWebApplication and not CConsoleApplication

Oh, right. Since the host name is read from the request, you’d have to do some tricks here anyway (there’s no request on a console app). My approach would be to study the createAbsoluteUrl() code and create and configure a custom “dummy” HttpRequest class that provides all data required by the url manager.

Were you able to solve this issue? As of now, I’m hard-coding the link link but looking for a better solution.



this was changed in 1.1.8, allowing url creation in console applications

Thanks for the update.

I’m still having trouble creating an absolute url in a console app.

With 1.1.8 there are no errors when calling either Yii::app()->createAbsoluteUrl or Yii::app()->createUrl.

However, both createUrl’s and createAbsoluteUrl’s output is pointing to the yiic script (console script file); instead of apache document root.


echo Yii::app()->createAbsoluteUrl('user/verify', array('code' => $verification_code), 'https');

// Outputs: 

// https://C:\xampplite\htdocs\project_browser\code\private\yiic.php?r=user/verify&code=7d08e4be74742bf1e3f481bc06ab0c5d

echo Yii::app()->createUrl('user/verify', array('code' => $verification_code));

// Outputs :

// C:\xampplite\htdocs\project_browser\code\private\yiic.php?r=user/verify&code=7d08e4be74742bf1e3f481bc06ab0c5d

I’ve tried setting the baseUrl property but there’s no change in output.


Have any ideas what I’m doing wrong?



No idea

Everything looks right

Looks like a bug to me, I can’t test here tho

I know this post is quite old. I came across the same problem. I used the following workaround:

In your config, define your URL in the params section

Then you can use

<?php echo Yii::app()->params['my_url'] . CHtml::normalizeUrl(array('/controller/action')); ?>

Gustavo’s right. I ended up solving it like this.


        'request' => array(

            'hostInfo' => 'https://localhost',

            'baseUrl' => '/some/local/folder/structure',

            'scriptUrl' => '',



        'request' => array(

            'hostInfo' => '',

            'baseUrl' => '',

            'scriptUrl' => '',


Console Email Template file

echo Yii::app()->createAbsoluteUrl('site/passwordVerify', array('code' => $verification_code));



today i come across the same problem too :lol: , but luckily i come to this thread , thanks all of you

in webApplication you can use XXXController to generate the request config for consoleApplication , here is my code :

//my console config under the protected/config/console dir 

  //request object config:

  'request' =>  require( __DIR__ . '/requestConfig.php'),



   * this action used to generate the request config file for console app


 public function actionGenRequestConfig4ConsoleApp(){

         $consoleRequestConfigPath = Yii::getPathOfAlias('application.config.console.'). DIRECTORY_SEPARATOR .'requestConfig.php';

         $config = require($consoleRequestConfigPath);

         $config['hostInfo'] = Yii::app()->request->getHostInfo();

        $config['baseUrl'] = Yii::app()->getBaseUrl();

        $config['scriptUrl'] = Yii::app()->request->getScriptUrl();



//my  saveArray2file function:

static public function saveArray2file(array $data, $dir, $fileName)


        $cache_path = $dir . DIRECTORY_SEPARATOR . $fileName . '.php';

        if (!$fp = fopen($cache_path, 'wb')) {

            return FALSE;


        if (flock($fp, LOCK_EX)) {

            //fwrite($fp, serialize($data));

            fwrite($fp, "<?php\nreturn " . var_export($data, true) . ";");

            flock($fp, LOCK_UN);

        } else {

            return FALSE;



        @chmod($cache_path, 0777);

        return TRUE;


hope this help some one

Iuse ssl just in login use ssl add following code in actionLogin in sitecontroller:

public function actionLogin()


                //use SSL in login form

                if(!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on')


                      header('Location: https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);





		$model=new LoginForm;

		// if it is ajax validation request

		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')


			echo CActiveForm::validate($model);



		// collect user input data




			// validate user input and redirect to the previous page if valid

			if($model->validate() && $model->login())



		// display the login form



hope it usefull. :rolleyes:

Thanks for you reply. That’s right~

Thanks for your answers . It’s awesome .

My solution is dublicate urlManager component to cron.php config (I am using "php cron.php" to call console commands).