在网站项目中,没必要把公用的函数写成一个工具类,有时候面向过程其实更方便。
在入口文件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. Django-Form 补充

    一.Form的前端循环 class LoginForm(Form): name = ... pwd = ... def func(request): form = LoginForm() return ...

  2. 在Xcode6中搭建Python开发环境

    http://jingyan.baidu.com/article/f71d60379c73001ab741d14e.html 对于使用Mac电脑,刚入门Python的同学,一定很好奇是不是可以使用Xc ...

  3. (十四)git操作

    https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

  4. SuSE Linux10.1 网络设置以及和主机通信

    SuSE Linux10.1 网络设置以及和主机通信 http://www.cnblogs.com/kevintian/articles/1086994.html 在VMWare上安装好SuSE之后, ...

  5. Spring MVC的静态和动态概念

    MVC模式: 图释:用户请求通过HTTP协议到达Front controller,Front controller把请求送给Controller,Controller了解业务逻辑细节并且调用业务逻辑数 ...

  6. 非旋转Treap——普通平衡树

    扔板跑…… #include<bits/stdc++.h> #define N 100010 #define mp make_pair using namespace std; typed ...

  7. ZJOI2006书架

    追yql做题记录的时候做到的……一道Splay模版题…… 啊LCT写久了都有点忘了Splay了(什么奇怪的逻辑?) 其实说白了五个操作: 1. 将某元素置顶:将元素旋到根,然后将左子树合并到该元素的后 ...

  8. Android xmlpull 方式解析xml文件

    1.新建一个xml文件,放在res/xml目录下 <?xml version="1.0" encoding="utf-8"?> <citys& ...

  9. Docker容器进入的4种方式(转)

    这个文章不错,几种方式及使用范围都讲得清楚. 另外,还有一个文章说了为什么docker中并不合适ssh服务安装的情况. 我现在也是用第四种方式, docker exec -it [docker容器ID ...

  10. 大话PHP设计模式

    设计模式 一书将设计模式引入软件社区,该书的作者是 Erich Gamma.Richard Helm.Ralph Johnson 和 John Vlissides Design(俗称 “四人帮”).所 ...