Events and Hooks System

Hi,

I'm evaluating frameworks for my next project and I really like what I'm seeing here.

So I was wondering, are there any plans for an "Events and Hooks" system like this one for Kohana:

http://learn.kohanap…ooks-in-kohana/

Or maybe the capability exists and I just overlooked it.  I saw that someone created a THook component, but I'm not sure about exactly what that is.

Thanks,  Greg

Yes, it is supported.

See component event and component behavior

Thanks, I'll have a look.

First, hello to all and thanks for this great framework! I’ve been using it since version 1.0 .

The only downside for now is the lack of documentation: There’s sooooo many nice features, but maybe 20% is well documented with examples (very important, the examples :P) But this is normal since the framework is relatively new.

So, getting to the point:

I was using CodeIgniter before switching to Yii. CI have a ‘hooks’ feature that allows an action class to be executed at different points : pre controller, post controller, pre system, etc…

http://codeigniter.com/user_guide/general/hooks.html

Is there any equivalent feature in Yii ? I read many pages in the Class Reference : CModule, CApplication, Filters, etc… etc… but I just can’t find something that does the same job as CI’s hooks.

qiang, you say that Yii have this feature. So, how exactly do we use it ? Is there something that I can add in the config file that will tell Yii to load a class before EVERY controller ? An example please !!

Thanks!

http://www.yiiframework.com/doc/cookbook/44/

samdark, with the cookbook u sent and also with this one:

http://www.yiiframework.com/doc/cookbook/39/

i’m starting to understand… however, it says :

(If you need additional functionality, you may want to create a separate file for these functions.)

So let’s say I put this in my config file:

‘onBeginRequest’=>create_function(‘MyClass’, ‘MyMethod’),

Where should I have the MyClass.php class, and what structure should it have? Must MyClass extend another class?

Do I just create something like:

class MyClass {

function MyMethod {

… bla bla

}

}

and save it under protected/extensions/MyClass.php ??

That would be possible. You could also:


MyClass extends CApplicationComponent {


  public $someParameter='defaultvalue';


  public function init() {

    parent::init();

    // connect event handlers here.

  }


  // more methods here

}

and in your config:


  'preload' => array('mycomponent'),


  'components' => array(




    'mycomponent'=>array(

      'class' => 'MyClass',

      'someParameter' => 'someValue',

    ),

Mike, your example is clear, except for one thing I didn’t get: // connect event handlers here.

what do you mean by connect the handlers ?

Also, do I have to add any code in the controllers for the component to be loaded ? Or will it automatically be loaded before any controller ?

If some code has to be added in each controller, then events are not what I was looking for…

I’d like to be able to define some global variables in the preloaded class, and be able to call them from any controller using $this->myVariable, WITHOUT having to do any extra code in the controllers before calling these variables.

thanks for your help

edit: after some reading, I think what I need is to override the beforeControllerAction() method in config file. I’ll continue looking into it.


  public function init() {

    parent::init();


    // connect event handlers here.

    Yii::app()->OnBeginRequest=array($this,'doSomething');


  }


  public function doSomething() {

    // Do, something that needs to be done OnBeginRequest

  }



The trick here is, to identify the right object and event you want to listen to. But maybe that’s not at all what you really want. (see below)

All components from the components array of your configuration will be loaded when accessed for the first time. Except those listed in preload: They’ll be loaded before, in the specified order. So: No additional code necessary. Or put another way: Creating and adding components with Yii is very easy. ;)

So maybe the base controller is what you are looking for. Not sure when it was introduced but in 1.1 when you create a webapp, you already have this base controller in protected/components. It’s the base class that all your controllers should extend from. You can add global properties/methods there that should be available from every controller. It also provides a init() method for e.g. initializing some vars.

I created my WebApp using Yii 1.02, and the controllers were extending CController.

So I manually added the BaseController under /components, and renamed all controller extends from CController to BaseController. Thanks alot Mike, ur replies are extremely clear.

While i’m here (or maybe u prefer I start a new thread?) is there a way to subdivide models in subfolders and have them all load automatically, without having to modify the main.php config file to import each subfolder ?

maybe newer releases of Yii already support subfolders for models ?

No i don’t think so. One option might be to add you own autoloader that helps finding your models. But i’m not so sure if that’s advisable. I never had to change it. Maybe this cookbook gives you some inspiration:

http://www.yiiframework.com/doc/cookbook/37/