本文實例總結了Yii程式開發常見呼叫技巧。分享給大家參考,如下:
1. 設定cookie:
$cookie = new CHttpCookie('mycookie','this is my cookie'); $cookie->expire = time()+60*60*24*30; //有限期30天 Yii::app()->request->cookies['mycookie']=$cookie;
2. 讀cookie:
$cookie = Yii::app()->request->getCookies(); echo $cookie['mycookie']->value;
4 . 取得更新的資料id
$cookie = Yii::app()->request->getCookies(); unset($cookie[$name]);
5. 取得插入的資料id
$post->save(); $id = $post->attributes['id'];
6. 獲取get,post . 取得ip位址
$id = Yii::app()->db->getLastInsertID();
8. 取得上一頁的url 回傳
Yii::app()->request->getParam('id');
9. 取得目前url
Yii::app()->request->userHostAddress;
11 . 得到目前return url :
Yii::app()->request->urlReferrer;
12. 專案路徑:
Yii::app()->request->url;
13. . 在view中得到目前controller的ID方法:
Yii::app()->homeUrl;
Yii::app()->user->returnUrl;
dirname(Yii::app()->BasePath);
Yii::app()->request->baseUrl;