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.'));
}
}
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.