How to get controller inside component

Tried different time points to get the controller, but it`s always null.

This is how I try to access it:

Yii::app()->controller

I tried to recieve it onEndRequest event and also in init(). Both resolves to null.

I need to do some session-based state handling inside component which depends on the controller at hand.

The component must be loading before the controller does. Is this component set to autoload? Why do you need the controller called from the component? This is not common

onEndRequest is certainly not before controller:)

Actually why is it that so uncommon? Even if we take authorization, this also depends on controller name.

In my case I delete search query when not in specific set of pages.

onEndRequest() is a method of CApplication, not CComponent… Putting onEndRequest() in a component won’t do anything. Unless you mean you are calling the component in onEndRequest(). How is the component called?

Did you try to use Yii::app()->getController() ?

yes I tried using getController and i registered component method as onendrequest handler. If you look in the code it resets app controller back to $oldcontroller, but $oldcontroller is NULL. why is this swap necessary?

The swap I believe your talking about is required in the cases where a component is used, and the controller needs to be the current controller. OnEndRequest there is no active controller so you should get null.

You probably want to override YiiApp:afterControllerAction()

nz