yii模板中常用變數總結_PHP教程

來源:互聯網
上載者:User

yii模板中常用變數總結


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

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

則通過如下方式擷取的值對應分別為:

除網域名稱外的首頁地址

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'); 
 
在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

得到proteced目錄的實體路徑 Yii::app()->basePath;

項目路徑 dirname(Yii::app()->basePath) 

您可能感興趣的文章

  • Yii framework架構之模組開發分析
  • Yii rules常用驗證規則備忘
  • Yii架構Yiiapp()的理解
  • Thinkphp 模板中常用的系統變數總結
  • Yii CDbCriteria的常用方法總結
  • Yii中的資料庫事務的使用方法小結
  • smarty模板中使用php函數以及smarty模板中如何對一個變數使用多個函數
  • Yii常用路徑方法總結

http://www.bkjia.com/PHPjc/1076539.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1076539.htmlTechArticleyii模板中常用變數總結 yii模板中常用的一些變數總結。 現有這樣一個url:http://www.phpernote.com/demos/helloworld/index.php/xxx/xxx 則通過如下方式獲...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.