elite核心类库之事件类
观察者模式?
事件允许你在不修改核心代码而将自己编写的代码到其他的代码中,因为这个是面向开发者而非终端用户,所以没有GUI来管理这些功能,取而代之的是通过修改配置文件来注册事件。
保存事件数据有两种方法:使用数据库或文件。我们选用基于文件的方式。
事件的注册:
为了事件能够工作,首先必须在config/events.php文件中注册,通过以数组形式添加你代码的信息。每个事件只需要知道模块、控制器、类库、helper的名字就可以。
示例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
$config['event_name'][] = array( 'module' => 'module_name', 'filepath' => 'controllers', 'filename' => 'my_event.php', 'class' => 'MyEvent', 'method' => 'my_method' );
创建自己的事件
你可以在自己的模块中创建并使用事件,他们将与系统事件具有同样的优先级。可以简单的在你自己的控制器或类库中调用trigger()方法
Events::trigger('event_name', $payload);
当方法被调用时,他将在事件数组中搜索匹配的事件并运行。大多数系统事件将提供一个负载器Most system events will deliver a payload. 典型的,这将是一个允许你操作的数组类型的数据. 每个负载器将以下述的方式描述事件。
系统事件
控制器:
before_controller | 在控制器构造函数之前调用,负载器是当前的控制器的名字. |
after_controller_constructor | 在控制器构造方法执行后但在方法执行前调用, 负载器是当前控制器的名字. |
模板和页
after_page_render | Called just after the main view is rendered. Payload is the view’s output. |
after_layout_render | Called just after the entire page is rendered. Payload is the current output. |
after_block_render | Called just after the block is rendered. Payload is an array with ‘block’ being the name of the block and ‘output’ being the rendered block’s output. |
Users
after_login | Called after successful login. Payload is an array of ‘user_id’ and ‘role_id’. |
before_logout | Called just before logging the user out. Payload is an array of ‘user_id’ and ‘role_id’. |
after_create_user | Called after a user is created. Payload is the new user’s id. |
before_user_update | Called just prior to updating a user. Payload is an array of ‘user_id’ and ‘data’, where data is all of the update information passed into the method. |
after_user_update | Called just after updating a user. Payload is an array of ‘user_id’ and ‘data’, where data is all of the update information passed into the method. |
具体功能后续补全
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)