站内搜索

yii教程——Ⅴ 、扩展 Yii

 了使这些档案通过 Web访问,我们需要用 CWebApplication::assetManager 发布他们,例如上述代码段所示。此外,如果我们想包括CSS或JavaScript文件在当前的网页,我们需要使用CClientScript 注册 : class MyWidget extends CWidget { protected functionregisterClientScript() { // ...publishCSS or JavaScriptfile here...
$cs=Yii::app()->clientScript; $cs->registerCssFile($cssFile); $cs->registerScriptFile($jsFile);
}
} 小工具也可能有自己的视图文件。如果是这样,创建一个目录命名 views 在包括小工具类文件的目录下,并把所有的 视图文件放里面。在小工具类中使用$this->render('ViewName') 来 render 渲染小工具视图,类似于我们在控制器里做。
3 3 3 3、Action Action Action Action(动作)
action 应继承 CAction 或者其子类。action 要实现的主要方法是 IAction::run 。
4 4 4 4、Filter Filter Filter Filter(过滤器)
filter 应继承 CFilter 或者其子类。filter 要实现的主要方法是 CFilter::preFilter 和 CFilter::postFilter。前者是在 action 之前 被执行,而后者是在之后。 class MyFilter extends CFilter { protected functionpreFilter($filterChain) { // logicbeing applied before theaction is executed returntrue; // false ifthe actionshould not be executed }
protected functionpostFilter($filterChain) { // logicbeing applied afterthe action is executed }
} 参数$filterChain 的类型是 CFilterChain,其包含当前被 filter 的 action 的相关信息。
5 5 5 5、Controller Controller Controller Controller(控制器)
controller 要作为扩展需继承 CExtController,而不是 CController。主要的原因是因为 CController 认定控制器视图文件 位于 application.views.ControllerID 下,而 CExtController 认定视图文件在 views 目录下,也是包含控制器类目录的一个 子目录。因此,很容易重新分配控制器,因为它的视图文件和控制类是在一起的。
6 6 6 6、Validator Validator Validator Validator(验证)
Validator 需继承 CValidator 和实现 CValidator::validateAttribute 方法。 class MyValidatorextends CValidator { protected functionvalidateAttribute($model,$attribute) { $value=$model->$attribute; if($value has error) $model->addError($attribute,$errorMessage);
}
}
7 7 7 7、Console Console Console ConsoleCommand Command Command Command(控制台命令)
console command 应 继 承 CConsoleCommand 和 实 现 CConsoleCommand::run 方 法 。 或 者 , 我 们 可 以 重 载 CConsoleCommand::getHelp 来提供一些更好的有关帮助命令。 class MyCommandextends CConsoleCommand { public function run($args) { // $args gives an array ofthe command-line arguments forthis command }
public function getHelp() { return'Usage: howtouse this command'; }
}
8 8 8 8、Module Module Module Module(模块)
请参阅 modules 一节中关于就如何创建一个模块。 一般准则制订一个模块,它应该是独立的。模块所使用的资源文件(如 CSS , JavaScript ,图片),应该和模块一起 分发。还有模块应发布它们,以便可以 Web 访问它们 。
9 9 9 9、Generic Generic Generic GenericComponent Component Component Component(通用组件)
开发一个通用组件扩展类似写一个类。还有,该组件还应该自足,以便它可以很容易地被其他开发者使用。
三、使用第三方库 三、使用第三方库 三、使用第三方库 三、使用第三方库
Yii 是精心设计的,使第三方库可易于集成,进一步扩大 Yii 的功能。 当在一个项目中使用第三方库,程序员往往遇到 关于类命名和文件包含的问题。 因为所有 Yii 类以 C 字母开头,这就减少可能会出现的类命名问题;而且因为 Yii 依赖 SPLautoload 执行类文件包含,如果他们使用相同的自动加载功能或 PHP 包含路径包含类文件,它可以很好地结合。 下面我们用一个例子来说明如何在一个 Yiiapplication 从 Zendframework 使用 Zend_Search_Lucene 部件。 首先,假设 protected 是 applicationbase directory,我们提取 ZendFramework 的发布文件到 protected/vendors 目录 。 确 认 protected/vendors/Zend/Search/Lucene.php 文件存在。 第二,在一个 controller 类文件的开始,加入以下行: Yii::import('application.vendors.*'); require_once('Zend/Search/Lucene.php'); 上述代码包含类文件 Lucene.php。因为我们使用的是相对路径,我们需要改变 PHP 的包含路径,以使文件可以正确定 位。这是通过在 require_once 之前调用 Yii::import 做到。 一旦上述设立准备就绪后,我们可以在 controller action 里使用 Lucene 类,类似如下: $lucene=new Zend_Search_Lucene($pathOfIndex); $hits=$lucene->find(strtolower($keyword));

  • 上一篇:yii教程——Ⅳ、缓存
  • 下一篇:php邮件发送类,使用smtp发送