yii框架中常用變量、函數使用總結

yii模板中常用的一些變量總結。

現有這樣一個url:http://www.kubiji.cn/demos/helloworld/index.php/xxx/xxx

則通過如下方式獲取的值對應分別爲:

除域名外的首頁地址

HTML " data-pbcklang="html" data-pbcktabsize="4">
echo Yii::app()->user->returnUrl;  //  '/demos/helloworld/index.php'

當前頁面url

echo Yii::app()->request->url;  //  '/demos/helloworld/index.php/xxx/xxx'

當前域名

echo Yii::app()->request->hostInfo;  //  'http://www.phpernote.com/'

根目錄URL

echo Yii::app()->request->baseUrl;  //  '/demos/helloworld'

除域名外的根目錄地址

echo Yii::app()->homeUrl;  //   '/demos/helloworld/index.php'

創建url地址

echo Yii::app()->createUrl('Site'); //  /demos/helloworld/index.php?r=Site

除域名外的URL

Yii::app()->request->getUrl();

跳轉前一個頁面url 

$this->redirect(Yii::app()->request->urlReferrer);

跳轉當前頁面url 

Yii::app()->request->redirect(Yii::app()->user->returnUrl);

創建url地址

 Yii::app()->createUrl('/',array('param1'=>'val'));  //  /demos/helloworld/index.php

渲染視圖(佈局) 

$this->render('view', array('attribute1'=>'value1','attribute2'=>'value2'));

跳轉頁面

 $this->redirect(array('route','attribute1'=>'value1','attribute2'=>'value2'));

創建小組件

$this->beginWidget(string $className, array $properties=array ( ))
$this->endWidget();

局部渲染 

renderPartial('view', array('attribute1'=>'value1','attribute2'=>'value2'));

調用YII框架中jquery:

Yii::app()->clientScript->registerCoreScript('jquery');   

framework/web/js/source的js,其中registerCoreScriptkey調用的文件在framework/web/js/packages.php列表中可以查看
 
在view中得到當前controller的ID方法:

Yii::app()->getController()->id;    

在view中得到當前action的ID方法:

Yii::app()->getController()->getAction()->id;    

yii獲取ip地址:

Yii::app()->request->userHostAddress; 

yii判斷提交方式:

Yii::app()->request->isPostRequest 

得到當前域名: 

Yii::app()->request->hostInfo  

得到proteced目錄的物理路徑:

YII::app()->basePath;   

獲得上一頁的url以返回:

Yii::app()->request->urlReferrer;

得到當前url :

Yii::app()->request->url;

得到當前home url :

Yii::app()->homeUrl

得到當前return url :

Yii::app()->user->returnUrl

項目路徑:

dirname(Yii::app()->BasePath)

Yii framework 已經定義的命名空間常量

system: 指向Yii框架目錄;  Yii\framework

zii: 指向zii library 目錄;  Yii\framework\zii

application : 指向應用程序基本目錄;  protected\

webroot: 指向包含裏入口腳本 文件的目錄;  .\

ext : 指向包含所有第三方擴展的目錄;  \protected\extensions
 
用法:

Yii::getPathOfAlias('webroot')

取得當前的完整路徑

Yii::getFrameworkPath()  :YII framework路徑

插入meta信息

Yii::app()->clientScript->registerMetaTag('keywords','關鍵字');
Yii::app()->clientScript->registerMetaTag('description','一些描述');
Yii::app()->clientScript->registerMetaTag('author','作者');

表示爲:

Yii::app()->clientScript->registerLinkTag('alternate','application/xml',$this->createUrl('/feed'));


 在控制器添加CSS文件或javascript文件

Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl.'/css/my.css');
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl.'/css/my.js');  
 


 Yii獲取ip地址

Yii::app()->request->userHostAddress;  

 
Yii判斷提交方式

Yii::app()->request->isPostRequest

得到proteced目錄的物理路徑 

Yii::app()->basePath;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章