Yii Framework Forum: small suggestion @ CHttpRequest - Yii Framework Forum

Jump to content

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

small suggestion @ CHttpRequest @ getQuery() & getPost() Rate Topic: -----

#1 User is offline   saegeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 245
  • Joined: 09-December 09
  • Location:Montpellier - France

Posted 09 April 2010 - 10:59 AM

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

And [for] their saying, "Indeed, we have killed the Messiah, Jesus, the son of Mary, the messenger of God ." And they did not kill him, nor did they crucify him; but [another] was made to resemble him to them. And indeed, those who differ over it are in doubt about it. They have no knowledge of it except the following of assumption. And they did not kill him, for certain.Rather, God raised him to Himself.
0

#2 User is offline   vamp 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 181
  • Joined: 30-January 09

Posted 09 April 2010 - 03:40 PM

View Postsaegeek, on 09 April 2010 - 10:59 AM, said:

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)
-Posted Image-
Thanks, you make me stronger!
0

#3 User is offline   saegeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 245
  • Joined: 09-December 09
  • Location:Montpellier - France

Posted 09 April 2010 - 11:11 PM

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.
And [for] their saying, "Indeed, we have killed the Messiah, Jesus, the son of Mary, the messenger of God ." And they did not kill him, nor did they crucify him; but [another] was made to resemble him to them. And indeed, those who differ over it are in doubt about it. They have no knowledge of it except the following of assumption. And they did not kill him, for certain.Rather, God raised him to Himself.
0

#4 User is offline   vamp 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 181
  • Joined: 30-January 09

Posted 10 April 2010 - 12:44 AM

View Postsaegeek, on 09 April 2010 - 11:11 PM, said:

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 :rolleyes:
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',
	),
),

-Posted Image-
Thanks, you make me stronger!
0

#5 User is offline   saegeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 245
  • Joined: 09-December 09
  • Location:Montpellier - France

Posted 10 April 2010 - 10:04 AM

<?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.
And [for] their saying, "Indeed, we have killed the Messiah, Jesus, the son of Mary, the messenger of God ." And they did not kill him, nor did they crucify him; but [another] was made to resemble him to them. And indeed, those who differ over it are in doubt about it. They have no knowledge of it except the following of assumption. And they did not kill him, for certain.Rather, God raised him to Himself.
0

#6 User is offline   Y!! 

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

Posted 10 April 2010 - 03:26 PM

View Postsaegeek, on 10 April 2010 - 10:04 AM, said:

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

#7 User is offline   saegeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 245
  • Joined: 09-December 09
  • Location:Montpellier - France

Posted 11 April 2010 - 11:07 AM

View PostY!!, on 10 April 2010 - 03:26 PM, said:

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?


The "required" attribute @ model()->rules already checks if the field is empty or not.
And [for] their saying, "Indeed, we have killed the Messiah, Jesus, the son of Mary, the messenger of God ." And they did not kill him, nor did they crucify him; but [another] was made to resemble him to them. And indeed, those who differ over it are in doubt about it. They have no knowledge of it except the following of assumption. And they did not kill him, for certain.Rather, God raised him to Himself.
1

#8 User is offline   jayrulez 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 348
  • Joined: 29-July 09

Posted 11 April 2010 - 11:54 AM

The current behaviour is fine. You can check if your variable is empty where you use it.
php:
foreach(array('cat', 'dog', 'cow') as $animal) echo $animal."\n";

python:
[(animal, print(animal)) for animal in ['cat', 'dog', 'cow']]

ruby:
['cat', 'dog', 'cow'].each {|animal| puts animal}


You say Tomato, I say Tomato.
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