Yii Framework Forum: Re-writing a Class Method without editing Framework Files - Yii Framework Forum

Jump to content

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

Re-writing a Class Method without editing Framework Files Rate Topic: -----

#1 User is offline   xgarb 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 19-July 10

Posted 24 December 2010 - 02:00 PM

Is is possible to alter a particular method in a framework class by extending it and Yii automatically use that new method?

I've edited CHttpRequest.php in the framework folder (no no no.. I know!) so my application would accept the CSRF token via GET. I understand the concept of extending a class with new methods and placing it in the 'components' dir but.. what if I want to re-write a method and have it used instead of the original framework method?

Re-written method below but my question is a general one rather than specific to this.

	public function validateCsrfToken($event)
	{
		if($this->getIsPostRequest())
		{
			$cookies=$this->getCookies();
			if($cookies->contains($this->csrfTokenName) && isset($_POST[$this->csrfTokenName]) || isset($_GET[$this->csrfTokenName] ))
			{
				$tokenFromCookie=$cookies->itemAt($this->csrfTokenName)->value;
				$tokenFrom=!empty($_POST[$this->csrfTokenName]) ? $_POST[$this->csrfTokenName] : $_GET[$this->csrfTokenName];		
				$valid=$tokenFromCookie===$tokenFrom;
			}
			else
				$valid=false;
			if(!$valid)
				throw new CHttpException(400,Yii::t('yii','Lite: The CSRF token could not be verified.'));
		}
	}

0

#2 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 24 December 2010 - 07:09 PM

class EHttpRequest extends CHttpRequest
{
 	public function validateCsrfToken($event)
        {
                if($this->getIsPostRequest())
                {
                        $cookies=$this->getCookies();
                        if($cookies->contains($this->csrfTokenName) && isset($_POST[$this->csrfTokenName]) || isset($_GET[$this->csrfTokenName] ))
                        {
                                $tokenFromCookie=$cookies->itemAt($this->csrfTokenName)->value;
                                $tokenFrom=!empty($_POST[$this->csrfTokenName]) ? $_POST[$this->csrfTokenName] : $_GET[$this->csrfTokenName];       	
                                $valid=$tokenFromCookie===$tokenFrom;
                        }
                        else
                                $valid=false;
                        if(!$valid)
                                throw new CHttpException(400,Yii::t('yii','Lite: The CSRF token could not be verified.'));
                }
        }}

Put EHttpRequest in components.
Tell Yii to use that instead of the ordinary one.
Haven't tested this, but this ought to do it:
	'components' => array(
    	'request' => array(
        	'class' => 'application.components.EHttpRequest',
    	),
),

"Less noise - more signal"
0

#3 User is offline   xgarb 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 19-July 10

Posted 28 December 2010 - 01:01 PM

Cool, works for me. First line needs to be

class EHttpRequest extends CHttpRequest


for anyone copy and pasting.
0

#4 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 28 December 2010 - 01:29 PM

Error fixed - thanks. :)
"Less noise - more signal"
0

#5 User is offline   Trejder 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,197
  • Joined: 06-October 10
  • Location:Southern Poland

Posted 28 December 2010 - 04:52 PM

View Postjacmoe, on 24 December 2010 - 07:09 PM, said:

Put EHttpRequest in components.
Tell Yii to use that instead of the ordinary one.

Good question, if this will work for any core class, not being a component? And if not, then what is the best practice to achieve it?

Got the very same issue when writing this Wiki article. Ended with modifying core Yii files, even though I know this is a very bad manner. But had not time investigating then, if there is any way to overload original Yii class not being a component.
Proud Cookbook author, though still learning powerful Yii! :] See my generic profile for more information. Cheers!
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