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’);

$controller->createAbsoluteUrl(‘Site/contact’);

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?

Thanks!

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

Thanks,

Matt

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

http://code.google.com/p/yii/source/browse/trunk/CHANGELOG?spec=svn3350&r=3350#70

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.

Examples:




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.




Yii::app()->request->setBaseUrl('https://localhost/project_browser/html/cp.host4hope.com/index-local.php');



Have any ideas what I’m doing wrong?

Thanks,

Matt

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.

console-local.php





        'request' => array(

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

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

            'scriptUrl' => '',

        ),



console.php







        'request' => array(

            'hostInfo' => 'https://domainname.com',

            'baseUrl' => '',

            'scriptUrl' => '',

        ),



Console Email Template file





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



Cheers,

Matt

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();


        ArrayUtil::saveArray2file($config,Yii::getPathOfAlias('application.config.console.'),'requestConfig');

    }


//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;

        }

        fclose($fp);

        @chmod($cache_path, 0777);

        return TRUE;

    }




hope this help some one

Iuse ssl just in login form.to 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']);

                       exit;

                 }

                 

                 

		$model=new LoginForm;


		// if it is ajax validation request

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

		{

			echo CActiveForm::validate($model);

			Yii::app()->end();

		}


		// collect user input data

		if(isset($_POST['LoginForm']))

		{

			$model->attributes=$_POST['LoginForm'];

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

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

				$this->redirect(Yii::app()->user->returnUrl);

		}

		// display the login form

		$this->render('login',array('model'=>$model));

	}



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