• Yii使用公共函数


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

     

  • 相关阅读:
    20145337《网络对抗技术》逆向及BOF基础
    20145337 《信息安全系统设计基础》课程总结
    微信小程序——3、逻辑js文件
    微信小程序——2、配置json文件
    微信小程序——1、文件的认识
    20145336 张子扬 《网络对抗技术》 web安全基础实践
    20145336 张子扬 《网络对抗技术》web基础
    20145336张子扬 《网络对抗技术》 网络欺诈技术防范
    20145336《网络对抗技术》Exp6 信息搜集技术
    20145336张子扬《网络对抗》MSF基础应用
  • 原文地址:https://www.cnblogs.com/grimm/p/5414735.html
Copyright © 2020-2023  润新知