small suggestion @ CHttpRequest

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)

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',

	),

),


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

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?

The "required" attribute @ model()->rules already checks if the field is empty or not.

The current behaviour is fine. You can check if your variable is empty where you use it.