Extend CHttpRequest

I’m sure this is simple, but until recently I haven’t needed to override much in the framework.

Now I need to override CHttpRequest::normalizeRequest() so I created a class EHttpRequest and placed it in ‘application.components’.

In main.php config is this, so the app is looking in the location where the class is:




	'import'=>array(

		'application.models.*',

		'application.components.*',

	),



However, during a trace where the request fails, I can see the app referencing CHttpRequest::normalizeRequest() instead of my overridden function in EHttpRequest.

I can add the ‘class’ key to the ‘request’ component as below but then of course all other request functions will fail.




	'request'=>array(

            'class'=>'EHttpRequest',

            'enableCsrfValidation'=>true,

        ),



The overridden class is straightforward…




class EHttpRequest extends CHttpRequest

{

	protected function normalizeRequest()

	{

	    // normalize request

	    if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())

	    {

	        if(isset($_GET))

	            $_GET=$this->stripSlashes($_GET);

	        if(isset($_POST))

	            $_POST=$this->stripSlashes($_POST);

	        if(isset($_REQUEST))

	            $_REQUEST=$this->stripSlashes($_REQUEST);

	        if(isset($_COOKIE))

	            $_COOKIE=$this->stripSlashes($_COOKIE);

	    }

	

	    if($this->enableCsrfValidation)

            ...

}



I’m doing something stupid but I can’t see what it is…how do I do it?

Look at CHttpRequest and see if the function declaration differs.

If it does, you are adding instead of overriding. :)

I checked the class reference and it looks the same. To be sure, I also checked the local source on my server (1.1.2.r2086); it’s also the same (below):




       /**

         * Normalizes the request data.

         * This method strips off slashes in request data if get_magic_quotes_gpc() returns true.

         * It also performs CSRF validation if {@link enableCsrfValidation} is true.

         */

        protected function normalizeRequest()

        {

                // normalize request

                if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())

                {

                        if(isset($_GET))

                                $_GET=$this->stripSlashes($_GET);

                        if(isset($_POST))

                                $_POST=$this->stripSlashes($_POST);

                        if(isset($_REQUEST))

                                $_REQUEST=$this->stripSlashes($_REQUEST);

                        if(isset($_COOKIE))

                                $_COOKIE=$this->stripSlashes($_COOKIE);

                }


                if($this->enableCsrfValidation)

                        Yii::app()->attachEventHandler('onBeginRequest',array($this,'validateCsrfToken'));

        }



They will not fail, the methods in the parent class (CHttpRequest) will be used. The config you posted looks good, try it that way.

It failed before with the ‘class’ key but that turned out to be my normalizeRequest() :-[ , which I guess is why the trace still showed CHttpRequest::validateCsrfToken().

Working now. Thank you both for the assist!