yii模板中经常使用变量总结

yii模板中经常使用的一些变量总结。php

现有这样一个url:http://www.phpernote.com/demos/helloworld/index.php/xxx/xxxcss

则经过以下方式获取的值对应分别为:jquery

除域名外的首页地址web

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

当前页面url框架

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

当前域名布局

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

根目录URLurl

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) 

相关文章
相关标签/搜索