How to create a common action for all controllers?

How to create a common action for all controllers? E.g. a statistics tracker that will add data about every visit to the database.


Look at the guide:

Sound more like you’d need a behavior rather than an action for all your controllers.

All your controller extends Controller class . You can find Controller class in /protected/components/ folder. Controller class extends CController class of yii framework.

If you add a method in you /protected/controller/Controller.php file, the method will be visible in all your controller.

You can also create a reusable action. But I think extends Controller class is better.

You can try to create a filter (to me it looks similar to the "performance filter" described in the Guide).

[color="#006400"]/* Moved from Installation forum to Yii 1.1 Help forum */[/color]

Thanks to all of you guys!

I chose the following solution. In the main controller I’ve put beforeAction() function:

protected function beforeAction($action) {

/* ... Do something common for all child controllers here ... */

return true;


I seems like this way is most useful.

Thanks a lot for posting your solution, I searched this type of solution for many days but not found. :)

This worked also for me ! thanks man ! :)