在网站项目中,没必要把公用的函数写成一个工具类,有时候面向过程其实更方便。
在入口文件index.php里添加
require_once('protected/function.php');
即可对其引用,成为公用的函数集合。
function.php如下:
<?php
/**
  * This is the shortcut to DIRECTORY_SEPARATOR
  */
defined('DS') or define('DS',DIRECTORY_SEPARATOR);
 
defined('TIME') or define('TIME', time());
 
defined('MTIME') or define('MTIME', intval(microtime(true)));//返回当前unix时间戳
/**
  * This is the shortcut to Yii::app()
  */
function app()
{
 return Yii::app();
}
 
/**
  * This is the shortcut to Yii::app()->clientScript
  */
function cs()
{
     // You could also call the client script instance via Yii::app()->clientScript
     // But this is faster
     return Yii::app()->getClientScript();
}
 
/**
  * This is the shortcut to Yii::app()->user.
  */
function user()
{
     return Yii::app()->getUser();
}
 
/**
  * This is the shortcut to Yii::app()->createUrl()
  */
function url( $route , $params = array (), $ampersand = '&' )
{
     return Yii::app()->createUrl( $route , $params ,$ampersand );
}
 
/**
  * This is the shortcut to CHtml::encode
  */
/* function h( $text )
{
     return htmlspecialchars( $text ,ENT_QUOTES,Yii::app()->charset);
} */
 
/**
  * This is the shortcut to Yii::app()->request->baseUrl
  * If the parameter is given, it will be returned and prefixed with the app baseUrl.
  */
function baseDir( $url =null)
{
     //static $baseUrl = null;
     //if ( $baseUrl ===null)
     $baseUrl =Yii::app()->getRequest()->getBaseUrl();
     return $url ===null ?  $baseUrl :  $baseUrl . '/' .ltrim( $url , '/' );
}
 
/**
  * Returns the named application parameter.
  * This is the shortcut to Yii::app()->params[$name].
  */
function param( $name )
{
     return Yii::app()->params[ $name ];
}
/**
  * A useful one that I use in development is the following
  * which dumps the target with syntax highlighting on by default
  */
function dump( $target )
{
   return CVarDumper::dump( $target , 10, true) ;
}
 
function mk_dir($dir, $mode = 0777)
{
 if (is_dir($dir) || @mkdir($dir,$mode)) return true;
 if (!mk_dir(dirname($dir),$mode)) return false;
 return @mkdir($dir,$mode);
}
 
//自定义更多函数...

Yii使用公共函数的更多相关文章

  1. ThinkPHP 3.2.3 自动加载公共函数文件的方法

    方法一.加载默认的公共函数文件 在 ThinkPHP 3.2.3 中,默认的公共函数文件位于公共模块 ./Application/Common 下,访问所有的模块之前都会首先加载公共模块下面的配置文件 ...

  2. CodeIgniter 引入自定义公共函数

    CodeIgniter 中公共函数不能追加,可以通过 helper 辅助函数实现. 创建 common_helper.php 文件,定义所需公共函数,存放至 application/helpers 目 ...

  3. ThinkPHP第三天(公共函数Common加载,dump定义,模板文件,定义替换__PUBLIC__)

    1.公共函数定义 自动加载:在项目的common文件夹中定义,公共函数文件命名规则为common.php,只有命名成common.php才能被自动载入. 动态加载:可以修改配置项‘LOAD_EXT_F ...

  4. 【ASP.NET】怎样使用类创建公共函数,在不同ASP.NET页面间反复调用

    为了降低代码冗余,应将公共函数写在类中,供不同ASP.NET页面调用. 1,先新建一个类,并在类中加入函数逻辑 namespace public_function_demo { public clas ...

  5. postman定义公共函数

    对于postman定义公共函数,相信很多小伙伴并不是很了解,下面给大家带来了一点福利,一起来看看吧.1.判断是否超时(assertNotTimeout)

  6. Postman高级应用——流程控制、调试、公共函数、外部数据文件

    postman客户端下载地址:https://www.getpostman.com/apps 目录 流程控制 调试 公共函数 外部数据文件 流程控制 流程控制简言之就是设置接口的执行顺序,流程控制只有 ...

  7. JavaScript进阶(一)抽离公共函数

    JS抽离公共函数 问题 在经历了"大量"的项目开发后,发觉越来越多的方法可以被抽离出来作为一个公共方法使用.那么,在js中该思想又该如何实现呢? 解答 例如,以下方法用于实现将标准 ...

  8. larave5.4自定义公共函数的创建

    原文地址:http://blog.csdn.net/qq_38125058/article/details/76862151 公共函数,简单来说就是在任何地方都可以直接使用这个函数.简单介绍两种实现方 ...

  9. phpcms公共函数库 总结

    *  global.func.php 公共函数库 /** * 返回经addslashes处理过的字符串或数组 * @param $string 需要处理的字符串或数组 * @return mixed ...

随机推荐

  1. HDU3910(数学期望题,题目难懂)

    Liang Guo Sha Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  2. Codeforces 362E Petya and Pipes 费用流建图

    题意: 给一个网络中某些边增加容量,增加的总和最大为K,使得最大流最大. 费用流:在某条边增加单位流量的费用. 那么就可以2个点之间建2条边,第一条给定边(u,v,x,0)这条边费用为0 同时另一条边 ...

  3. Linux驱动 读写文件【转】

    转自:http://blog.csdn.net/h_armony/article/details/7546624 在VFS的支持下,用户态进程读写任何类型的文件系统都可以使用read和write着两个 ...

  4. Cause: org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: java.sql.SQLException: 不支持的特性

    mybatis插入数据时报错: Cause: org.apache.ibatis.executor.ExecutorException: Error getting generated key or ...

  5. Unicode(UTF-8, UTF-16)令人混淆的概念----我看完了 不错

    来自:http://www.cnblogs.com/kingcat/archive/2012/10/16/2726334.html ---------------------------------- ...

  6. 深入理解Java的注解(Annotation):基本概念(1)

    zz自:http://www.cnblogs.com/peida/archive/2013/04/23/3036035.html 什么是注解(Annotation): Annotation(注解)就是 ...

  7. spring quartz job autowired 出错 null pointer

    什么情况下才能用autowired? 当当前类属于spring IOC容器管时候就可以,比如在applicationContext.xml里有定义 就是说在spring上下文里能够找到 但是比如qua ...

  8. [libgdx游戏开发教程]使用Libgdx进行游戏开发(4)-素材管理

    游戏中总是有大量的图像资源,我们通常的做法是把要用的图片做成图片集,这样做的好处就不多说了.直接来看怎么用. 这里我们使用自己的类Assets来管理它们,让这个工具类作为我们的资源管家,从而可以在任何 ...

  9. zookeeper与activemq整合

    (1)zookeeper与activemq原理 使用ZooKeeper实现的Master-Slave实现方式,是对ActiveMQ进行高可用的一种有效的解决方案,高可用的原理:使用ZooKeeper( ...

  10. (21)python Anaconda

    1.安装 Anaconda下载镜像      --官网下载太慢 https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/ 下载安装后 2.添加第三方源 管 ...