zoukankan      html  css  js  c++  java
  • Yii framework 应用总结小窍门(转)

    1. Yii Framework] 如何获取当前controller的名称? 
    下面语句就可以获取当前控制器的名称了! 

    Php代码  收藏代码
    1. Yii::app()->controller->id  



    2. yii 如何使用第三方插件 
    第一,比如说,我们要使用 Zend framework的东西。我们把zend framework解压到 prtected/vendors里面,现在的文件夹为 protected/vendors/Zend/Search/Lucene.php 

    第二,在controller文件的头部,插入下面代码。 
    Yii::import('application.vendors.*'); 
    require once('Zend/Search/Lucene.php'); 
    上面代码包含了Lucene.php这个类文件。因为我们用到的是相对路径,所以我们需要改变PHP加载文件的路径,Yii::import 一定要在require_once 之前。 

    第三,一旦我们设置好了,我们就可以在controller里面使用了。比如说 
    $lucene=new Zend Search Lucene($pathOfIndex); 
    $hits=$lucene->find(strtolower($keyword)); 

    3. yii中如何在查询的时候使用数据库函数 
    比如要使用mySQL中的md5函数, 
    Test::model()->findAll(new CDbExpression("md5(name) =1")); 

    4. yii的controller中外挂action 
    创建 

    Php代码  收藏代码
    1. class UpdateAction extends CAction {  
    2.   public function run() {  
    3.     // place the action logic here  
    4.   }  
    5. }  


    调用 

    Php代码  收藏代码
    1. class PostController extends CController {  
    2.   public function actions() {  
    3.     return array'edit'=>'application.controllers.post.UpdateAction', );  
    4.   }  
    5. }  



    5. Yii创建widget 

    Php代码  收藏代码
    1. class MyWidget extends CWidget {  
    2.   public function init() {  
    3.     // this method is called by CController::beginWidget()  
    4.   }  
    5.   public function run() {  
    6.     // this method is called by CController::endWidget()  
    7.   }  
    8. }  


    通常,widget的视图是是放在components/views里面的,通过CWidget::render()来传递参数的 

    6. CWidget::init()与CWidget::run()的联系 
    要创建一个新的挂件(widget),我们主要是要继承两个方法:CWidget::init()和 CWidget::run(), 

    CWidget::init 调用是发生在我们使用 $this->beginWidget 将挂件插入到一个view里面, 
    CWidget::run 调用是发生在我们使用 $this->endWidget 这个方法的时候。 
    如果我们想捕捉和处理两者之间的方法核查办上显示的内容,我们可以在CWidget::init开始输出缓冲,然后在CWidget::run中检索缓冲输出 
    并作进一步处理。 

    7. Yii如何使用theme 
    在main.php 里面配置 
    return array( 
      'theme'=>'basic', 
      //...... 
    ); 
    要使用theme里面的资源的话,比如说images, js, css, 应该这样, Yii::app()->theme->baseUrl.”/images/FileName.gif” 
    Yii::app()->Theme->baseUrl.”/css/default/common.css” 

    8.Yii 如何在当前页面注册css和js文件 
      $cs=Yii::app()->clientScript; 
      $cs->registerCssFile($cssFile); 
      $cs->registerScriptFile($jsFile); 

    9.Yii Captcha验证码的使用方法 
    假设使用的model名字为Comment 
    Model里面 

    Php代码  收藏代码
    1. public function rules() {  
    2.   return array(  
    3.     ......  
    4.     array('verifyCode',  
    5.            'captcha',   
    6.            'on' => 'insert',  
    7.           'allowEmpty' => !Yii::app()->user->isGuest || !extension_loaded('gd')),  
    8.     );  
    9. }  



    View里面 
    <form action=”/test/xyz” method=”post”> 
      <input type=”text” name=”comment[verifyCode]”/> 
    </form> 
    Controller里面 
    public function xyz() { 
      $comment = new Comment; 
      $comment->validate('insert'); 
      //因为是insert的时候才会用到captcha,所以要加上参数'insert' 


    10. 如何调用extension扩展 
    Components的方法 
    引入以及定义: 
    在config.php文件里面 

    Php代码  收藏代码
    1. 'components'=>array(  
    2.   'xyz'=>array(  
    3.     'class'=>'ext.xyz.XyzClass',  
    4.     'property1'=>'value1',  
    5.     'property2'=>'value2',  
    6.   ),  
    7. // other component configurations  
    8. ),  


    使用方法: 
    在任何地方,使用Yii::app()->xyz,就可以直接使用xyz这个component了,而component的加载方式是 lazilycreated的,只要我们不是在preload=array()里面定义,那么就是,当第一次使用的时候,才会实例化的,所以不用担心说把 它放在config.php里面会影响性能。 

    11. Yii 数据保存时自动插入createTime和updateTime 
    Yii 1.1 version之后,可以直接这样: 

    Php代码  收藏代码
    1. public function behaviors(){  
    2.   return array(  
    3.     'CTimestampBehavior' => array(  
    4.       'class' => 'zii.behaviors.CTimestampBehavior',  
    5.       'createAttribute' => 'create_time_attribute',  
    6.       'updateAttribute' => 'update_time_attribute',  
    7.     )  
    8.   );  
    9. }  


    如果model里面已经在使用public function behaviors(),记得要在前面加上parent::behaviors($on); 

    12. Yii 数据库查询找出最新5个发布的内容 
    在数据查询的时候,出现下面的是什么意思? 
    $posts=Post::model()->published()->recently()->findAll(); 
    这个是叫做named scope, 
    每个命名范围被声明为一个可以被用来初始化CDbCriteria阵列实例。 
    如要下面的例子 

    Php代码  收藏代码
    1. class Post extends CActiveRecord {  
    2.   ......  
    3.   public function scopes() {  
    4.     return array(  
    5.       'published'=>array(  
    6.         'condition'=>'status=1',  
    7.       ),  
    8.       'recently'=>array(  
    9.         'order'=>'createTime DESC',  
    10.         'limit'=>5,  
    11.       ),  
    12.     );  
    13.   }  
    14. }  


    而$posts=Post::model()->published()->recently()->findAll();的意思就是找出最新的status为1的post的5条记录 

    13. 在views里面如何调用本controller的方法,获取一定的值 
    直接在views里面使用$this->method(),如 
    controller里面: 

    Php代码  收藏代码
    1. class PostController extends Ccontroller {  
    2.    public function actionList(){....}  
    3.    public function getTitle(){return 'test title';}  
    4. }  


    views的list.php 
    <?php echo $this->getTitle();?> 
    这样就可以调用本controller的方法了 

    14. Yii framework已经定义的命名空间常量 
    system: Yii framework directory 
    application: application's base directory 
    webroot: the directory containing the entry script file 
    ext: directory of extensions 

    system: 指向 Yii 框架目录; 
    zii: 指向 zii library 目录; 
    application: 指向应用程序 基本目录(base directory); 
    webroot: 指向包含里 入口脚本 文件的目录. 此别名自 1.0.3 版起生效. 
    ext: 指向包含所有第三方扩展的目录, 从版本 1.0.8 可用; 

    15. yii中如何不加载layout 
    可以使用renderPartial()来代替render() 

    16. yii中向widget传值 
    $this->widget('CMaskedTextField',array('mask'=>'99/99/9999')); 

    来自:第五实验室(http://www.5labs.net) 

    From: http://koda.iteye.com/blog/1134712

  • 相关阅读:
    HTML元素盒模型
    对git使用的初步总结
    在windows上,使用虚拟机安装苹果操作系统
    C++多例模式下对Instance的使用
    C++中的Inline函数的使用
    gitbash安装与使用
    C++中的Inline函数
    git bash下添加忽略文件列表
    C++数据类型(data type)介绍
    RFID会议签到系统总结(六)――系统配置(下)
  • 原文地址:https://www.cnblogs.com/imxiu/p/3414806.html
Copyright © 2011-2022 走看看