If you preload the component, any call you do from it is too early for controllers because at that point the controller has not been instantiated, therefore you can’t hook into it.
If yii 1.1 would have global events that wouldn’t need an object to attach to then it wouldn’t be a problem, but it doesn’t, therefore you cannot achieve this out-of-the-box.
What you can do instead, is to create your own component for attaching callbacks to your own handlers.
Here is a simple example to help you get started
class EventsManager extends CApplicationComponent
{
private $_events;
protected function getEvents()
{
if (!($this->_events instanceof CMap)) {
$this->_events = new CMap();
}
return $this->_events;
}
public function addEvent($tag, $callback)
{
if (empty($tag) || !is_callable($callback, true)) {
return $this;
}
if (!$this->getEvents()->contains($tag)) {
$this->getEvents()->add($tag, new CList());
}
$this->getEvents()->itemAt($tag)->add($callback);
return $this;
}
public function triggerEvent($tag, $arg = null)
{
if (!$this->getEvents()->contains($tag)) {
return $this;
}
$args = func_get_args();
$args = array_slice($args, 2);
array_unshift($args, $arg);
$events = $this->getEvents()->itemAt($tag)->toArray();
foreach ($events as $eventCallback) {
call_user_func_array($eventCallback, $args);
}
return $this;
}
}
This is a component, preload it as "eventsManager" for example.
Then, from your other components you will do something like:
public function anyComponentMethod()
{
[...]
// add the callback
Yii::app()->eventsManager->addEvent('my_custom_event_name', array($this, 'myCallback'));
[...]
}
// and add the callback
public function myCallback($controllerInstance) {
// $controllerInstance is the controller instance, do whatever with it
$controllerInstance->themeName = 'bar';
}
then in your controllers you would have something like:
public $themeName = 'foo';
public function actionIndex()
{
[...]
Yii::app()->eventsManager->triggerEvent('my_custom_event_name', $this);
[...]
Yii::app()->theme = $this->themeName;
$this->render('whatever');
}
And this way the controller would trigger a callback that you have registered earlier in the application.
This is the same approach that wordpress does with add_action().
Hope it helps.
Thanks 