Executing a specified function for each request

I'm trying to figure out how to call a specific statuc function each time a Yii page is loaded. It seems like a simple thing but I can't figure out how to do it without modifying base framework files.

I tried making a master Controller class (MasterController) that every other controller extends, but there doesn't seem to be a way to include the MasterController.php file after CController is loaded, but before MypageController is loaded. Does anyone know if that is possible?

Here's what I've got:

<?php

class SomeClass

{

  public static function someFunction()

  {

      // do things

  }

}

?>

All I want is for SomeClass::someFunction() to be called no matter what controller is used. I can manually put the function call inside init() for controller, but that seems like a really ugly way to do it…

I hope that all makes sense… Thanks in advance for any help you can give.



Take a look at init() callback.


Thanks. I’ve already looked at that, the problem is that each controller needs to have the init specified. What I’m after is a solution that lets me specify that each controller should call the required function, without having to manual copy and paste the code into each controller.

If the function doesn’t require the request to have been processed (i.e. route and action determined), you can place your function call in your config/main.php.

This file is processed with each page load and can easily run your function.

what about a function that triggers on beginRequest?

doc

see this