Execute custom code for each controller action

Hi,

I want to be able to check if the user is logged in for all controller actions. The reason I want to do this is that for a user to be able to use a site, he must be logged on at all cases. No anonymous access is allowed. I have tried to override the run() method of my controller and it seems to work:

This snippet works. Two questions:

  1. Is overriding the run() method of each controller the correct way?

  2. Is there somewhere where I can execute my custom code for all controllers in the site?

Thanks.

You can define a base controller class and all your controllers would extend from this base class. Then you can put your code in beforeAction() or run() in your base controller class.

Thanks Qiang, that was helpful!

Hi,

On this topic I was wondering why it was chosen not to enable this by default.

Having a BaseController created when a webapp is generated via yiic ?

Thanks