Subscribers in Yii2?

Hi,

I’ve just imagine if there is any posibility to set Subscribers in Yii2 like Symphony does? Or how to work with events instead? Is there any substitute?

Anyone? ;>

I’m not sure what subsribers are. Everything about events is documented: http://www.yiiframework.com/doc-2.0/guide-concept-events.html

I mean eventSubscribers EventDispatcher?

?

There I can just create SomeSubscriber implements EventSubscriber and register function on events… There I don’t see that possibility.

Maybe you better describe what you want to do instead of how so we can tell you how to do it in yii.

So as i wrote before:

I want to create a class where I can call function on events. It would be a class that I dont need to call anywhere but framework would do it for me…

In symphony I create class with Subcriber suffix implements event interface where I call function on events. That easy ;]

sorry for my english so far :P

I see. If you want to handle it in centralized way you can do it like the following:




class EventHandler extends Component

{

    public function init()

    {

        Yii::$app->on('x', [$this, 'handleX']);

    }


    public function handleX($event)

    {

        // handle it

    }

}



then in your config you need to add this class to framework bootstrap so it will register handlers before any event actually happen.

ok I see but maybe a little help with addint it to bootstrap? ;]

http://www.yiiframework.com/doc-2.0/guide-structure-application-components.html#bootstrapping-components