edanb
(Edanbarak)
January 5, 2011, 6:16pm
1
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)
mikl
(Mike)
January 6, 2011, 9:57am
2
Why not use Yii::app()->createAbsoluteUrl() ?
edanb
(Edanbarak)
January 6, 2011, 4:19pm
3
This is only defined for CWebApplication and not CConsoleApplication
mikl
(Mike)
January 7, 2011, 8:39am
4
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
gusnips
(Gustavo)
July 10, 2011, 12:33pm
6
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
gusnips
(Gustavo)
July 10, 2011, 4:13pm
8
No idea
Everything looks right
Looks like a bug to me, I can’t test here tho
befi
(Bernhard Finkbeiner)
September 29, 2011, 3:30pm
9
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
yiqing95
(Yiqing 95)
November 22, 2011, 8:23am
11
today i come across the same problem too , but luckily i come to this thread , thanks all of you
yiqing95
(Yiqing 95)
November 22, 2011, 9:33am
12
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
za_al
(Zakieh Alizadeh)
July 21, 2012, 1:17pm
13
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.
gaoxu387
(Run Gao2012)
August 14, 2013, 8:12am
14
Thanks for you reply. That’s right~
andybegin
(Zhexiao)
August 14, 2013, 7:33pm
15
Thanks for your answers . It’s awesome .
Briareos17
(Oleg Briareos17)
May 7, 2015, 9:52am
16
My solution is dublicate urlManager component to cron.php config (I am using "php cron.php" to call console commands).