Yii全局函数用法示例
框架(架构)  /  管理员 发布于 7年前   232
本文实例讲述了Yii全局函数用法。分享给大家供大家参考,具体如下: 由于YII致力于完美的整合第三方库,它并没有定义任何全局函数。yii中的每一个应用都需要全类别和对象范围。 例如,Yii::app()->user;Yii::app()->params['name'];等等。我们可以自行设定全局函数,使得代码看起来更加简洁易用。 我们可以保存在globals.php在protected/config目录下。然后,在入口脚本index.php中,定义如下内容: 现在我们可以在应用的任何地方使用我们的全局函数,例如可以使用user()代替Yii::app()->user。注:如果你打算发布一个可重用的组件,请不要组件中使用全局函数,在不同的应用配置中,可能导致无法使用。同时,也应注意与第三方库的冲突,可考虑对每个函数前加上自己的前缀,已做区分,例如框架核心均已C为前缀。 下面是代码包含最常用的一些快捷功能。如还需其他,请自行添加。 更多关于Yii相关内容感兴趣的读者可查看本站专题:《Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》 希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。$globals=dirname(__FILE__).'/protected/config/globals.php';...require_once($yii);require_once($globals);
/*** 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];}/*** 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);}
您可能感兴趣的文章:
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号