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) 

猜你喜欢

转载自blog.csdn.net/mo3408/article/details/81945394
今日推荐