Yii framework 應用小竅門
1. Yii Framework] 如何獲取當前controller的名稱?
下面語句就可以獲取當前控制器的名稱了!
Php代碼
- Yii::app()->controller->id
2. yii 如何使用第三方插件
第一,比如說,我們要使用 Zendframework的東西。我們把zend framework解壓到 prtected/vendors裏面,現在的文件夾爲 protected/vendors/Zend/Search/Lucene.php
第二,在controller文件的頭部,插入下面代碼。
- Yii::import('application.vendors.*');
- require once('Zend/Search/Lucene.php');
第三,一旦我們設置好了,我們就可以在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
創建
- class UpdateAction extends CAction {
- public function run() {
- // place the action logic here
- }
- }
調用
- class PostController extends CController {
- public function actions() {
- return array( 'edit'=>'application.controllers.post.UpdateAction', );
- }
- }
5. Yii創建widget
- class MyWidget extends CWidget {
- public function init() {
- // this method is called by CController::beginWidget()
- }
- public function run() {
- // this method is called by CController::endWidget()
- }
- }
通常,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裏面
- public function rules() {
- return array(
- ......
- array('verifyCode',
- 'captcha',
- 'on' => 'insert',
- 'allowEmpty' => !Yii::app()->user->isGuest || !extension_loaded('gd')),
- );
- }
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文件裏面
- 'components'=>array(
- 'xyz'=>array(
- 'class'=>'ext.xyz.XyzClass',
- 'property1'=>'value1',
- 'property2'=>'value2',
- ),
- // other component configurations
- ),
使用方法:
在 任何地方,使用Yii::app()->xyz,就可以直接使用xyz這個component了,而component的加載方式是 lazilycreated的,只要我們不是在preload=array()裏面定義,那麼就是,當第一次使用的時候,纔會實例化的,所以不用擔心說把 它放在config.php裏面會影響性能。
11. Yii 數據保存時自動插入createTime和updateTime
Yii 1.1 version之後,可以直接這樣:
- public function behaviors(){
- return array(
- 'CTimestampBehavior' => array(
- 'class' => 'zii.behaviors.CTimestampBehavior',
- 'createAttribute' => 'create_time_attribute',
- 'updateAttribute' => 'update_time_attribute',
- )
- );
- }
如果model裏面已經在使用public function behaviors(),記得要在前面加上parent::behaviors($on);
12. Yii 數據庫查詢找出最新5個發佈的內容
在數據查詢的時候,出現下面的是什麼意思?
- $posts=Post::model()->published()->recently()->findAll();
每個命名範圍被聲明爲一個可以被用來初始化CDbCriteria陣列實例。
如要下面的例子
- class Post extends CActiveRecord {
- ......
- public function scopes() {
- return array(
- 'published'=>array(
- 'condition'=>'status=1',
- ),
- 'recently'=>array(
- 'order'=>'createTime DESC',
- 'limit'=>5,
- ),
- );
- }
- }
而
- $posts=Post::model()->published()->recently()->findAll();
13. 在views裏面如何調用本controller的方法,獲取一定的值
直接在views裏面使用$this->method(),如
controller裏面:
- class PostController extends Ccontroller {
- public function actionList(){....}
- public function getTitle(){return 'test title';}
- }
views的list.php
- <?php echo $this->getTitle();?>
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'));