本文实例讲述了Yii全局函数用法。分享给大家供大家参考,具体如下:
由于YII致力于完美的整合第三方库,它并没有定义任何全局函数。yii中的每一个应用都需要全类别和对象范围。
例如,Yii::app()->user;Yii::app()->params['name'];等等。我们可以自行设定全局函数,使得代码看起来更加简洁易用。
我们可以保存在globals.php在protected/config目录下。然后,在入口脚本index.php中,定义如下内容:
$globals=dirname(__FILE__).'/protected/config/globals.php'; ... require_once($yii); require_once($globals);
现在我们可以在应用的任何地方使用我们的全局函数,例如可以使用user()代替Yii::app()->user。注:如果你打算发布一个可重用的组件,请不要组件中使用全局函数,在不同的应用配置中,可能导致无法使用。同时,也应注意与第三方库的冲突,可考虑对每个函数前加上自己的前缀,已做区分,例如框架核心均已C为前缀。
下面是代码包含最常用的一些快捷功能。如还需其他,请自行添加。
/** * 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 "_blank" href="https://www.jb51.net/Special/386.htm">Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- 银霞.1983-《你那好冷的小手》珍藏纪念版[WAV分轨]
- 银霞.2002-《国语巨星专辑VOL.3》2CD台湾版[WAV+CUE]
- 金池刘罡《昨日情歌HQ》头版限量编号[低速原抓WAV+CUE]
- 段奥娟.2024-23·岁碎念【白米范】【FLAC分轨】
- 顺子.2001-昨日·唯一·更多·顺子自选辑【魔岩】【WAV+CUE】
- 无印良品.1999-私藏三首·原创相对论(EP)【滚石】【WAV+CUE】
- Merryland《Merryland》[FLAC/分轨][170.22MB]
- 庄锭欣 ELKIE《没有你的未来》[320K/MP3][15.78MB]
- 庄锭欣 ELKIE《没有你的未来》[FLAC/分轨][36MB]
- TchaikovskySwanLake,Op.20柴可夫斯基天鹅湖(2013)SACD[Hi-Res]
- 24bit达人艺典《琴歌终极版》整轨WAV+CUE
- 黑鸭子.2005-《发烧DSD》柏菲[WAV分轨]
- 群星.1990-电影看板·人间道【宝丽金】【WAV+CUE】
- 周殷廷.2024-TO.WHOM.IT.MAY.CONCERN【环球】【FLAC分轨】
- 草蜢.1997-三人世界精选17首【宝丽金】【WAV+CUE】