加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

Yii全局函数用法示例

发布时间:2020-05-23 03:53:44 所属栏目:PHP 来源:互联网
导读:这篇文章主要介绍了Yii全局函数用法,结合实例形式分析了Yii全局函数的功能、定义与使用方法,需要的朋友可以参考下

本文实例讲述了Yii全局函数用法。分享给大家供大家参考,具体如下:

由于YII致力于完美的整合第三方库,它并没有定义任何全局函数。yii中的每一个应用都需要全类别和对象范围。

例如,user;Yii::app()->params['name'];等等。我们可以自行设定全局函数,使得代码看起来更加简洁易用。

我们可以保存在globals.php在protected/config目录下。然后,在入口脚本index.php中,定义如下内容:

现在我们可以在应用的任何地方使用我们的全局函数,例如可以使用user()代替Yii::app()->user。注:

下面是代码包含最常用的一些快捷功能。如还需其他,请自行添加。

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',$source = null,$language = null) { return Yii: :t($category,$message,$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]; } /** * 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); }

更多关于Yii相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》及《》

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读