Yii framwork 應用小竅門

Yii framework 應用小竅門

 

 

1. Yii Framework] 如何獲取當前controller的名稱? 
下面語句就可以獲取當前控制器的名稱了! 

Php代碼  

  1. Yii::app()->controller->id    


2. yii 如何使用第三方插件 
第一,比如說,我們要使用 Zendframework的東西。我們把zend framework解壓到 prtected/vendors裏面,現在的文件夾爲 protected/vendors/Zend/Search/Lucene.php 

第二,在controller文件的頭部,插入下面代碼。 
  1. Yii::import('application.vendors.*');   
  2. require once('Zend/Search/Lucene.php');   
上面代碼包含了Lucene.php這個類文件。因爲我們用到的是相對路徑,所以我們需要改變PHP加載文件的路徑,Yii::import 一定要在require_once 之前。 

第三,一旦我們設置好了,我們就可以在controller裏面使用了。比如說 
  1. $lucene=new Zend Search Lucene($pathOfIndex);   
  2. $hits=$lucene->find(strtolower($keyword));   

3. yii中如何在查詢的時候使用數據庫函數 
比如要使用mySQL中的md5函數, 
  1. 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 裏面配置 

  1. return array(   
  2.   'theme'=>'basic',   
  3.   //......   
  4. );   

要使用theme裏面的資源的話,比如說images, js, css, 應該這樣, 

  1. Yii::app()->theme->baseUrl.”/images/FileName.gif”   
  2. Yii::app()->Theme->baseUrl.”/css/default/common.css”   

8.Yii 如何在當前頁面註冊css和js文件   
  1. $cs=Yii::app()->clientScript;   
  2. $cs->registerCssFile($cssFile);   
  3. $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裏面 

  1. <form action=”/test/xyz” method=”post”>   
  2.   <input type=”text” name=”comment[verifyCode]”/>   
  3. </form>   
  4. Controller裏面   
  5. public function xyz() {   
  6.   $comment = new Comment;   
  7.   $comment->validate('insert');   
  8.   //因爲是insert的時候纔會用到captcha,所以要加上參數'insert'   
  9. }   



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個發佈的內容 
在數據查詢的時候,出現下面的是什麼意思? 

  1. $posts=Post::model()->published()->recently()->findAll();   
這個是叫做namedscope, 
每個命名範圍被聲明爲一個可以被用來初始化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. }    

  1. $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 

  1. <?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傳值 
  1. $this->widget('CMaskedTextField',array('mask'=>'99/99/9999'));   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章