Use Yii Events Like Wordpress


I want To know How Can i use events like hooks in Yii.


i have for example( CMenu widget which i want add Items during application load ) if user has enabled some modules.

i have some modules in application, each module wants to add Item to CMenu to add Extra Menu if it is Enabled.

But i dont know how use Events to Manipulate Items??

you may have to implements it yourself :lol:

use a intermediate table(or file) to save the publishers and the listeners .

when you want to trigger some event just select all listener .example table:

Create Table

CREATE TABLE `sys_hook` (


  `host_module` varchar(80) NOT NULL DEFAULT '''app''' COMMENT '宿主模块id',

  `hook_name` varchar(255) NOT NULL COMMENT '钩子名称executionPoint执行点 键值而已 如blogCreate',

  `client_module` varchar(80) NOT NULL DEFAULT '''app''' COMMENT '挂接方模块id',

  `client_hook_name` varchar(255) NOT NULL COMMENT '挂接方hook名字 用来删除的如blogOnUserDelete',

  `hook_content` text NOT NULL COMMENT '序列化或其他格式存储的hook内容自己定义解析格式',

  `priority` tinyint(5) NOT NULL DEFAULT '0' COMMENT '优先级',

  `type` varchar(25) NOT NULL DEFAULT '''custom''' COMMENT 'custome,action,filter',

  `create_time` int(11) NOT NULL,

  PRIMARY KEY (`id`),

  UNIQUE KEY `UNIQUE` (`client_hook_name`)


sorry for my chinese comments :D in the table schema

i meant something like wordpress hook or yii events

but i have checked yii events to edit variables but last event call affect data

If you intend to add header/footer elements through hooks, you can use the methods from CClientScript.registerX in the controller action:

  • registerMetaTag

  • registerCssFile

  • registerScriptFile

example usage:



Is this is what you wanted ?

or am i just shooting from the hip :blink:

i explain scenario again to clear it

i wordpress we have hooks which when we assign functions with add_action or filter to it we can change some data

forexample add_filter(‘the-content’,‘myfunc’);

function myfunc($data)


change $data to whathever you want


in this example we have one hook name the-content when we assign function like myfunc to it , this hook send $data which is data was generated for output to myfunc and assigned function can change it ,

it is what i want to implement in yii


modify Management Menu Items with hooks

i wish someone can help me and suggest some examples

what i give is what you want :lol:

in yii the event system is something like this :

first you should new(or init) the event source objects and then new(init) some event listeners

,then register the listener[s] to event source object .

later if you trigger some event ,the method of event listener will be called !

in yii CBehavior can listen some event of its “owner” .(if you are not familiar to this characteristic just look at the CActiveRecord 's event ,eg : afterXX beforXX … . and often we extends the CActiveRecordBehavior to listen these events )

the key point is that you must init or new both the event source object and the event listener eagerly .then manually register the listener to event source object .(if you use yii 's behavior just attach it to “owner” ).

but in wp . the thing is different .

when you call some method , we firstly to find if we register some hooks to this method . the register table is not in memory ,it often obtain by table or files .(the pub/sub design pattern can be implemented in different styles .)

lets say your example again:

in your view file ,you render a menu here we can define a hook point ,before you render it just trigger a event : ‘beforeXXMenuRender’. and some modules may listen to this event .if you use the yii 's event system you have to have the eventSource and eventListeners existed and hook it before the event triggered ! this means you should do some wasteful things (event may trigger or not be triggered ,and the module numbers may be unknown firstly .but you always do the registering eagerly !) . now we can realize it like this :

                $eventName = 'beforeRenderMyMenu';

                $params = array();


                //  now the menus will be collected from different modules .


                // after the event completed 

                $menus = $params['allMenus'];


                // render it 

here we should find the listeners which listen to the event by the eventName . the registry table may record the relations .

if you add some module latter and want to listen some event. just write to this "table".

you just consider how to realize such a global event system . the symfony evemtDispatcher may give you some idea .