Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inter module communication #28

Closed
mashhadi opened this issue Mar 25, 2013 · 7 comments
Closed

Inter module communication #28

mashhadi opened this issue Mar 25, 2013 · 7 comments

Comments

@mashhadi
Copy link

I am not sure what is the best place to put wish list, But it would be really help full if the framework provides some better approach for raising and handling events between different modules. Application level interrupts you can say.
like if a user save function is called, i can handle beforeSave afterSave of User model, in some other module.

@samdark
Copy link
Member

samdark commented Mar 25, 2013

Not quite sure what are you talking about exactly. Yii2 has event support i.e. you can do

$component->trigger('myEvent');

and then anything can subscribe to an event like the following:

$component->on('myEvent', function($event){ ... });

What's different from 1.1 is syntax and the fact that you don't need to declare event prior to raising it.

@samdark
Copy link
Member

samdark commented Mar 25, 2013

Moreover, with typical application flow there's only one module running at a time and we aren't going to change it.

@bwoester
Copy link
Contributor

👍 for inter module communication.

Would be really cool if we could do this from module 1:

Yii:app()->trigger( 'mod_1.xyz', new Mod1Event() );

and this in config or onAppInit():

Yii::app()->on('mod_1.xyz', function(Mod1Event $event) {
  Yii::app()->getModule('mod_2')->abc( $event->whatever );
});

And I think it should be possible with the new event system, shouldn't it?

@qiangxue
Copy link
Member

Yes, this is possible in Yii 2. You can define and use whatever events.

@mashhadi
Copy link
Author

cool

@yiqing-95
Copy link

@mashhadi i think i fully understand what 's your meaning !
in sns project there are some scenario need modules comunication ! say that in user module if a user be deleted and this action will affect some other modules like : blog ,photo , event .. . (if a user will be deleted ,you should also delete all of his/her resources)

even yii2 support new "event" model, the above scenario should be designed carefully . lets say yii2 's new event system:
Yii:app()->trigger( 'mod_1.whatEverStringNameAsEvent', new CEvent() );

Yii::app()->on('mod_1.whatEverStringNameAsEvent, function(CEvent $event) {
Yii::app()->getModule('mod_2')->abc( $event->whatever );
});
it can treat any string as a event id , not same as the event system of yii1.x (you must predefine a method as a event)

here i just give my way : you first define what event may be triggerred from one module , then use db to hold the listener list from another modules . when a module event fired the first thing is to look for all the listeners which listen to this event!
and then invoke these listeners one by one(surely you can define priority level to same eventId in the table).

for now yii2 (even sf1 eventDispatcher ) event system handle this thing is some thing like this : you first should register the listeners , you see you must "listen" the specific event before the event triggerred in same request thread ! but this may be wasteful if that event do not fire ( a event may be only triggerred on some specific condition ) . so even the yii2 support unpredefined event model , you will have to design it yourself to handle the module comunication !

in sns project the user related modules will not be foreknew (both the number and the content type !). usually these module can be installed and unistalled automatically , you can register the module event listeners in the install process ( you need insert records to module_event and module_listener table :) ,and do it in a reverse way in the uninstall process)!

@mashhadi
Copy link
Author

@yiqing-95 yes, i exactly mean the same thing, i have already implemented a similar approach but not involving any db, In every module i have a Event Handler class, i write all handlers in this class. Whenever an event is raised it check for EventHandler classes in all modules and call respective handlers. Your approach seems to be better as you can prioritize your handlers, but in my case that is not needed.
I really appreciate your thoughts, it will help me in future.

qiangxue pushed a commit that referenced this issue Oct 17, 2014
Translated structure-filters.md and structure-widgets.md. [skip ci]
samdark pushed a commit that referenced this issue Mar 30, 2018
Update concept-behaviors.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants