Yii 使用Widge面面观

我们可以把Widget视为一个嵌入到控制器管理 的视图中的微控制器,其实就是.net框架中的用户控件,或者类似于.net MVC中的子视图。controller相比较,微件没有既没有动作,也没有过滤器。

Yii 手册中提到Widget拥有自己的视图。

第一种方法 使用beginWidget 和endWidget

第二种方法 使用Widget('类名字')

如:

<?php $this->widget('HelloWidget'); ?>

自定义Widget:

继承 CWidget 以及重载它的init() 和 run() 方法,可以定义一个新的组件:

挂件的使用

面包屑导航

第一步 声明属性

One can define a property "breadcrumbs" in the base controller class and assign it to the widget in the layout, like the following: 
你可以定义一个breadcrumbs属性并且在布局文件中指派给(网站)基础控制器插件,如下所示:

$this->widget('zii.widgets.CBreadcrumbs', array(
   
'links'=>$this->breadcrumbs,
));

第二步 设置属性

Then, in each view script, one only needs to assign the "breadcrumbs" property as needed.
于是乎,你需要时,只需要在每个视图脚本中,指定breadcrumbs属性(就可以显示出网页导航了).
以上是官方提供的文档文件的介绍.
下面介绍视图文件中写法:

$this->breadcrumbs=array(

      'Users'=>array('index'),

      'Create',
     
// 形式 :  'key' =>'value'  key的位置相当于最后显示出来的a标签内的名字, value则相当于a标签的href属性.
     
// 'Create'表示当前页  故没有设置链接.
);
其他挂件

 参考

http://www.php100.com/manual/yii/CWidget.html#__construct-detail

原文地址:https://www.cnblogs.com/needrunning/p/3430559.html