php框架

 首页 > php框架 > YII > Yii使用公共函数

Yii使用公共函数

分享到:
【字体:
导读:
          在网站项目中,没必要把公用的函数写成一个工具类,有时候面向过程其实更方便。 在入口文件index.php里添加 require_once(protected/function.php); 即可对其引用,成为公用的函数集合。 func...

在网站项目中,没必要把公用的函数写成一个工具类,有时候面向过程其实更方便。
在入口文件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 Framework学习教程-YII的C-控制器
      设计模式中,MVC结构是使用最多的。现在大部分PHP框架的必备标准就是拥有MVC模式。这是最基本的要求。如果不具备这个要求,就不能称之为框架,只能说是一个工具类集合。M-V-C中是控制器,可以认为是MVC结构的核心,调度者,像一个国家的领导人。大部分程序的实现还是在此部分,(如果没有涉及的很多数据逻辑的时候...
yii 操作session
在 Yii框架中使用session 的笔记:首先,在Yii框架中,你不需要像标准PHP代码那样使用session_start(),在Yii框架中,autoStart 属性缺省被设置为true,所以,虽然没有使用session_start(),你仍然可以使用$_SESSION全局变量,但最好使用Yii框架封装的Yii::app->session:设置session变量:Yii::app()->session['var&#03...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……