初学Yii,刚看到其组件机制,特别是刚看“组件事件”的时候真是把人给看晕了,可能还是基础不好的吧........
接下来做个笔记备忘!!!
Yii的组件机制
组件机制,是Yii整个体系的思想精髓,在使用Yii之前,最应该先了解其组件机制,如果不了解这个机制,那么阅读Yii源代码会非常吃力。组件机制给Yii框架赋予了无穷的灵活性和可扩展性,可以毫不夸张地说,Yii框架的基础结构就是组件。大到CApplication对象、控制器、路由管理器(urlManager),小到一些其它插件,均是以组件形式存在的。
什么是Yii组件?
Yii中几乎所有可实例化并继承自CComponent的类,均可称为组件。
组件的特点是什么?
继承自CComponent类(直接继承或间接继承),拥有事件及行为机制,可在配置文件中定义其各个属性。
组件中的“事件”:
一个示例演示 组件事件如何运行。
-
-
-
-
- class ComponentTestController extends Controller
- {
-
-
-
-
-
- public function onSubmit($event){
-
-
- $this->raiseEvent("onSubmit", $event);
- }
-
-
-
-
-
- public function test1(){
- echo "这是在促发onSubmit时候执行的函数1<br />";
- }
-
- public function test2(){
- echo "这是在促发onSubmit时候执行的函数2<br />";
- }
-
- public function test3(){
- echo "这是在促发onSubmit时候执行的函数3<br />";
-
- }
-
- public function test4($data){
- echo "这是在促发onSubmit时候执行的函数4<br />";
- echo "<pre>";
- print_r($data->params);
- echo "</pre>";
- }
-
-
- public function actionIndex(){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- $this->onSubmit=array($this,"test3");
- $this->onSubmit=array($this,"test1");
- $this->onSubmit=array($this,"test2");
-
- $this->attachEventHandler("onSubmit", array($this,"test4"));
-
-
-
-
-
-
-
- $this->onSubmit(new CEvent($this,array("name"=>"参数一","sex"=>"参数二")));
-
- }
-
- }
运行结果如下:
- 这是在促发onSubmit时候执行的函数3
- 这是在促发onSubmit时候执行的函数1
- 这是在促发onSubmit时候执行的函数2
- 这是在促发onSubmit时候执行的函数4
- Array
- (
- [name] => 参数一
- [sex] => 参数二
- )
下面这个是看到别人的文章中对组件事件的使用示例。
- 例如我们希望当表单提交的时候,将此事件通知给日志组件让其记录之
定义一个form组件(是组件就必须直接或者间接的继承CComponent),并定义一个onSubmit事件(事件名称自己写啦...)
- class form extends CComponent {
- public function onSubmit($event) {
- $this->raiseEvent('onSubmit', $event);
- }
- }
在一段代码中实例化这个组件,比如在一个action中。
- class PostController extends Controller
- {
-
- public function actionUser{
-
- $form = new form();
-
- $logOjbect =new logOjbect();
-
-
- $form ->attachEventHandler( 'onSubmit', array($logOjbect, "saveLog") );
-
-
- $form->onSubmit(new CEvent($form, array('data'=>$_POST) ) );
-
- }
- }
-
-
- class logObject {
- public function saveLog($event) {
-
- $data=$event->params ;
-
- }
- }
组件中的“行为”:行为就是一个特殊的类。
一个行为的运行示例。
1.先定义一个行为类。
-
-
-
- class MyBehavior extends CBehavior {
-
- public $name="行为中的name属性<br />";
-
- public function test1(){
- echo "行为中的一个方法Test1<br />";
-
- }
- public function test2(){
- echo "行为中的一个方法Test2<br />";
- }
-
- public function test3(){
- echo "行为中的一个方法Test3<br />";
- }
-
- public function events() {
- return array(
- 'onSubmit' => 'test3',
- );
- }
- }
2.在一个类中(一个控制器组件或者其他地方,这里用控制器组件做示例)中使用上面定义的行为。
- Yii::import('application.behavior.MyBehavior');
- class BehaviorTestController extends Controller
- {
-
- public function onSubmit($event){
- $this->raiseEvent("onSubmit", $event);
- }
-
- public function test1(){
- echo "优先执行<br />";
- }
-
- public function actionIndex(){
-
- $this->attachBehavior('myapp','MyBehavior');
-
-
-
-
-
-
-
- echo $this->name;
- echo $this->test1();
- echo $this->test2();
-
-
-
- $this->onSubmit(new CEvent($this));
- }
- }
运行结果如下:
- 行为中的name属性
- 优先执行
- 行为中的一个方法Test2
- 行为中的一个方法Test3
一个具有实际应用价值的示例,用以表明什么时候会用到行为。
- 我们经常要对用户输入的内容进行过滤处理,比如防止其输入html标签,这种情况下,我们也可以考虑使用行为机制处理之
1.先定义一个html过滤行为类
- /**
- *定义一个标签过滤行为
- *
- /
- class myHtmlFilter extends CBehavior {
-
- public $strip_tags = false ;
-
- public function events() {
- return array(
- 'filter' => 'filterHtml',
- );
- }
-
-
- public function filterHtml($event) {
-
- if($event->sender instanceof CFormModel) {
- $input = $event->sender->attributes ;
- $event->sender->attributes = $this->filter($input);
- }
- }
-
- public function filter(&$data) {
- return is_array($data)?array_map(array($this, 'filter'),$data):strip_tags($data);
- }
- }
2.绑定行为,使用行为。
- $form = new FormModel;
- If($_POST) {
- $form->attributes = $_POST ;
-
-
- $form->attachBehavior('myFilter', array(
- 'class' => 'myHtmlFilter',
- 'strip_tags' => true ,
- ));
-
-
- $form->filter(new CEvent($form) ) ;
- }
总结:简单点,事件就是在事件这个方法被触发的时候可以自动的去执行被绑定好的一个或者一组方法(全局方法或者指定对象中的方法)。行为就是一个用以完成某种特殊功能的类。在组件(类)中用attachBehavior方法对行为进行绑定,绑定之后就可以再组件中使用行为中的所有方法和属性。
转自:http://blog.youkuaiyun.com/chensong0708/article/details/8140861?locationNum=8