I’d like to record the page execution to a database - I’ve generated a model and the code do what I want, but I don’t know where or how to attach the code. It needs to be run as the last thing in the Yii:app process so it can attain the logger’s execution time.
I have a model called VisitorLogModel, and created a new function within it that does the following:
public function LogVisit()
{
if (!Yii::app()->params['debug']):
$mdl_VisitorLog = new VisitorLogModel();
$mdl_VisitorLog->page_url = Yii::app()->getRequest()->url;
$mdl_VisitorLog->referrer_url = (isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : null;
$mdl_VisitorLog->yii_version= Yii::getVersion();
$mdl_VisitorLog->execution_time = (string)Yii::getLogger()->getExecutionTime();
$mdl_VisitorLog->insert();
unset($mdl_VisitorLog);
endif;
}
Where should I run this function? Where do I attach this behaviour/function call?
I do have a Controller called MasterController (which extends CController), which all my Controllers extend - so MasterController is run on every page load - would it be something I can put in here?
Thank you in advance!
Thanks to Troto for mentioning ‘onEndRequest’!