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模板中常用变量总结的更多相关文章

  1. django 模板中通过变量替代key取字典内容

    模板中通过变量替代key取字典内容 templatetags/├── get_item.py├── __init__.py ###get_item.py # coding=utf-8 from dja ...

  2. 走进AngularJs(二) ng模板中常用指令的使用方式

    通过使用模板,我们可以把model和controller中的数据组装起来呈现给浏览器,还可以通过数据绑定,实时更新视图,让我们的页面变成动态的.ng的模板真是让我爱不释手.学习ng道路还很漫长,从模板 ...

  3. 齐博x1模板中常用的TP标签数据处理

    上图是比较常用的, 而下图是比较特殊的场合,比如幻灯片可能会用到 下图使用了TP的循环标签. 上图只使用了条件判断标签 上图不存在 val="xxx" 这个参数,所以会自动循环输出 ...

  4. 在Intellij IDEA中修改模板中user变量名称

    在Intellij IDEA中的注释模板中的${user}名称是根据当前操作系统的登录名来取的,有时候登录名称和我们实际的user名称并不相同. 修改方法如下: 方法一:可以在settings的fil ...

  5. freemarker 模板中定义变量

    在模板中能够定义三种类型的变量: 简单变量:它能从模板中的不论什么位置来訪问,或者从使用 include 指令引入的模板訪问. 能够使用 assign 或 macro 指令来创建或替换这些变量. 局部 ...

  6. c++类模板中静态成员变量的声明定义

    我们知道,c++中,类的静态成员是要在.cpp文件中定义的,如果在.h中定义,会出现重复定义. 但是在写类模板时,一般所有的代码都是放在.h文件中的,如果要做分离是一件很麻烦的事.那如果出现了静态成员 ...

  7. [django]模板中自定义变量&django模板中的变量

    django自定义模板变量 context_processors.py def mysetings(request): return { 'NAME': 'maotai' } settings.py ...

  8. ShopEx 中规格属性添加时,自己主动计算其相应的销售价格,同一时候注意模板中的变量间的计算

    在ShopEx中,添加产品的规格时,如颜色.尺寸.是否送货等配置信息,默认情况下,这些内容是须要手动计算的,若仅仅有几个属性值还easy计算,假设每个属性值比較多,通过手动计算将是一个灰常巨大的工作量 ...

  9. ThinkPHP 3.1.2 模板中的变量

    一.变量输出 (重点) 1.标量输出 2.数组输出 {$name[1]} {$name['k2']} {$name.k1} 3.对象输出 {$name:k} {$name->k} 二.系统变量 ...

随机推荐

  1. 【转】MFC 程序入口和执行流程

    一 MFC程序执行过程剖析 1)我们知道在WIN32API程序当中,程序的入口为WinMain函数,在这个函数当中我们完成注册窗口类,创建窗口,进入消息循环,最后由操作系统根据发送到程序窗口的消息调用 ...

  2. 51nod——1391 01串(字符串、前缀和)

    好像这题是4级题下放2级? 预处理:求每个位置 x 左区间 [ 0 , x ] 中 1 比 0 多的数量和右区间 ( x , n - 1 ] 中 0 比 1 多的数量(少就是负的).相当于求两个前缀和 ...

  3. PHP实现同array_column一样的功能

    不用PHP自带的array_column函数实现同样的功能 <?php /** * Created by PhpStorm. * User: 123456 * Date: 2018/9/25 * ...

  4. Buffered Channels and Worker Pools

    原文链接:https://golangbot.com/buffered-channels-worker-pools/ buffered channels 带有缓冲区的channel 只有在缓冲区满之后 ...

  5. python操作日志的封装

    前言 曾经转载过一篇关于python日志模块logging的详解 https://www.cnblogs.com/linuxchao/p/linuxchao-log.html, 虽然这篇文章是别人写的 ...

  6. nrf51822微信开发入门学习笔记1:开始前的准备

    参考:(id:love--baby)https://blog.csdn.net/hunhun1122/article/details/68922493 微信硬件平台:https://iot.weixi ...

  7. Running OOM killer script for process 32248 for Solr on port 8983

    Running OOM killer script for process 32248 for Solr on port 8983 分析1 https://blog.csdn.net/qq_41665 ...

  8. LeetCode(226)Invert Binary Tree

    题目 分析 交换二叉树的左右子树. 递归非递归两种方法实现. AC代码 class Solution { public: //递归实现 TreeNode* invertTree(TreeNode* r ...

  9. poj 1742 多重背包问题 dp算法

    题意:硬币分别有 A1.....An种,每种各有C1......Cn个,问组成小于m的有多少种 思路:多重背包问题 dp[i][j]表示用前i种硬币组成j最多剩下多少个  dp=-1的表示凑不齐 dp ...

  10. Leetcode 814. 二叉树剪枝

    题目链接 https://leetcode-cn.com/problems/binary-tree-pruning/description/ 题目描述 给定二叉树根结点 root ,此外树的每个结点的 ...