zoukankan      html  css  js  c++  java
  • Yii 设置简写或者全局函数

    新建一个文件 globals.php  在里面可以加入自己想添加的函数

     然后在index.php 引用它

    require('path/to/globals.php');
    /**
     * This is the shortcut to DIRECTORY_SEPARATOR
     */
    defined('DS') or define('DS',DIRECTORY_SEPARATOR);
     
    /**
     * 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 CHtml::link()
     */
    function l($text, $url = '#', $htmlOptions = array()) 
    {
        return CHtml::link($text, $url, $htmlOptions);
    }
     
    /**
     * This is the shortcut to Yii::t() with default category = 'stay'
     */
    function t($message, $category = 'stay', $params = array(), $source = null, $language = null) 
    {
        return Yii::t($category, $message, $params, $source, $language);
    }
     
    /**
     * 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 bu($url=null) 
    {
        static $baseUrl;
        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];
    }
    

      原文:http://www.yiiframework.com/wiki/31/

  • 相关阅读:
    怎么知道银行卡号对应的银行
    集合排序、map、枚举
    669. Trim a Binary Search Tree修剪二叉搜索树
    17. Merge Two Binary Trees 融合二叉树
    226. Invert Binary Tree 翻转二叉树
    530.Minimum Absolute Difference in BST 二叉搜索树中的最小差的绝对值
    191. Number of 1 Bits 二进制中1的个数
    Hamming Distance二进制距离
    136. Single Number唯一的数字
    276. Paint Fence篱笆涂色
  • 原文地址:https://www.cnblogs.com/yxbs/p/3587338.html
Copyright © 2011-2022 走看看