saegeek
(Abdallah)
April 9, 2010, 3:59pm
1
I suggest to check if the var is empty as :
Orginal code :
<?php
public function getQuery($name,$defaultValue=null)
{
return isset($_GET[$name]) ? $_GET[$name] : $defaultValue;
}
public function getPost($name,$defaultValue=null)
{
return isset($_POST[$name]) ? $_POST[$name] : $defaultValue;
}
become :
<?php
public function getQuery($name,$defaultValue=null)
{
return isset($_GET[$name]) && !empty($_GET[$name]) ? $_GET[$name] : $defaultValue;
}
public function getPost($name,$defaultValue=null)
{
return isset($_POST[$name]) && !empty($_POST[$name]) ? $_POST[$name] : $defaultValue;
}
vamp
(Vamphouse)
April 9, 2010, 8:40pm
2
I suggest to check if the var is empty as :
Orginal code :
<?php
public function getQuery($name,$defaultValue=null)
{
return isset($_GET[$name]) ? $_GET[$name] : $defaultValue;
}
public function getPost($name,$defaultValue=null)
{
return isset($_POST[$name]) ? $_POST[$name] : $defaultValue;
}
become :
<?php
public function getQuery($name,$defaultValue=null)
{
return isset($_GET[$name]) && !empty($_GET[$name]) ? $_GET[$name] : $defaultValue;
}
public function getPost($name,$defaultValue=null)
{
return isset($_POST[$name]) && !empty($_POST[$name]) ? $_POST[$name] : $defaultValue;
}
It’s not valid logic, try to test:
$vals=array(0,null,"0",array());
foreach($vals as $v)
var_dump(empty($v));
But if you needs this trick - please, by extending base CHttpRequest class you can override getPost() and getQuery() methods (and provide class for "request" component in main.php)
saegeek
(Abdallah)
April 10, 2010, 4:11am
3
I mean we needs more often this trick than the default behavior.
@ URLs like
http://localhost/controller/action/foo/
$_GET[‘foo’] is set so our $_GET[‘foo’] = ‘’
$foo = Yii::app()->request->getQuery('foo','bar');
we prefer to get ‘bar’ more often.
vamp
(Vamphouse)
April 10, 2010, 5:44am
4
saegeek:
I mean we needs more often this trick than the default behavior.
@ URLs like
http://localhost/controller/action/foo/
$_GET[‘foo’] is set so our $_GET[‘foo’] = ‘’
$foo = Yii::app()->request->getQuery('foo','bar');
we prefer to get ‘bar’ more often.
You are - is not we
class HttpRequest extends CHttpRequest {
public function getQuery($name,$defaultValue=null)
{
return isset($_GET[$name]) && !empty($_GET[$name]) ? $_GET[$name] : $defaultValue;
}
public function getPost($name,$defaultValue=null)
{
return isset($_POST[$name]) && !empty($_POST[$name]) ? $_POST[$name] : $defaultValue;
}
}
in config/main.php:
'components'=>array(
'request'=>array(
'class'=>'HttpRequest',
),
),
saegeek
(Abdallah)
April 10, 2010, 3:04pm
5
<?php
public function getQuery($name,$defaultValue=null)
{
return isset($_GET[$name]) ? $_GET[$name] : $defaultValue;
}
public function getPost($name,$defaultValue=null)
{
return isset($_POST[$name]) ? $_POST[$name] : $defaultValue;
}
These 2 functions are totally useless because this is the same code as :
$foo = isset($_GET['foobar']) ? $_GET['foobar'] : 'bar';
But this :
return isset($_POST[$name]) && !empty($_POST[$name]) ? $_POST[$name] : $defaultValue;
is a bit more useful.
Y11
(Y!!)
April 10, 2010, 8:26pm
6
saegeek:
<?php
public function getQuery($name,$defaultValue=null)
{
return isset($_GET[$name]) ? $_GET[$name] : $defaultValue;
}
public function getPost($name,$defaultValue=null)
{
return isset($_POST[$name]) ? $_POST[$name] : $defaultValue;
}
These 2 functions are totally useless because this is the same code as :
$foo = isset($_GET['foobar']) ? $_GET['foobar'] : 'bar';
I’d rather do a function call instead of using ternary operator I think it’s not useless at all.
Regarding your suggestion, I don’t think that it makes sense. A GET or POST field can be empty. Maybe one want to check for that at some point?
saegeek
(Abdallah)
April 11, 2010, 4:07pm
7
The "required" attribute @ model()->rules already checks if the field is empty or not.
jayrulez
(Waprave)
April 11, 2010, 4:54pm
8
The current behaviour is fine. You can check if your variable is empty where you use it.