当前位置:Gxlcms > php框架 > Yii全局函数用法示例

Yii全局函数用法示例

时间:2021-07-01 10:21:17 帮助过:23人阅读

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

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

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

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

  1. $globals=dirname(__FILE__).'/protected/config/globals.php';
  2. ...
  3. require_once($yii);
  4. require_once($globals);

现在我们可以在应用的任何地方使用我们的全局函数,例如可以使用user()代替Yii::app()->user。注:如果你打算发布一个可重用的组件,请不要组件中使用全局函数,在不同的应用配置中,可能导致无法使用。同时,也应注意与第三方库的冲突,可考虑对每个函数前加上自己的前缀,已做区分,例如框架核心均已C为前缀。

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

  1. /**
  2. * This is the shortcut to DIRECTORY_SEPARATOR
  3. */
  4. defined('DS') or define('DS', DIRECTORY_SEPARATOR);
  5. /**
  6. * This is the shortcut to Yii::app()
  7. */
  8. function app() {
  9. return Yii: :app();
  10. }
  11. /**
  12. * This is the shortcut to Yii::app()->clientScript
  13. */
  14. function cs() {
  15. // You could also call the client script instance via Yii::app()->clientScript
  16. // But this is faster
  17. return Yii: :app() - >getClientScript();
  18. }
  19. /**
  20. * This is the shortcut to Yii::app()->user.
  21. */
  22. function user() {
  23. return Yii: :app() - >getUser();
  24. }
  25. /**
  26. * This is the shortcut to Yii::app()->createUrl()
  27. */
  28. function url($route, $params = array(), $ampersand = '&') {
  29. return Yii: :app() - >createUrl($route, $params, $ampersand);
  30. }
  31. /**
  32. * This is the shortcut to CHtml::encode
  33. */
  34. function h($text) {
  35. return htmlspecialchars($text, ENT_QUOTES, Yii: :app() - >charset);
  36. }
  37. /**
  38. * This is the shortcut to CHtml::link()
  39. */
  40. function l($text, $url = '#', $htmlOptions = array()) {
  41. return CHtml: :link($text, $url, $htmlOptions);
  42. }
  43. /**
  44. * This is the shortcut to Yii::t() with default category = 'stay'
  45. */
  46. function t($message, $category = 'stay', $params = array(), $source = null, $language = null) {
  47. return Yii: :t($category, $message, $params, $source, $language);
  48. }
  49. /**
  50. * This is the shortcut to Yii::app()->request->baseUrl
  51. * If the parameter is given, it will be returned and prefixed with the app baseUrl.
  52. */
  53. function bu($url = null) {
  54. static $baseUrl;
  55. if ($baseUrl === null) $baseUrl = Yii: :app() - >getRequest() - >getBaseUrl();
  56. return $url === null ? $baseUrl: $baseUrl.'/'.ltrim($url, '/');
  57. }
  58. /**
  59. * Returns the named application parameter.
  60. * This is the shortcut to Yii::app()->params[$name].
  61. */
  62. function param($name) {
  63. return Yii: :app() - >params[$name];
  64. }
  65. /**
  66. * A useful one that I use in development is the following
  67. * which dumps the target with syntax highlighting on by default
  68. */
  69. function dump($target) {
  70. return CVarDumper: :dump($target, 10, true);
  71. }

更多关于Yii相关内容感兴趣的读者可查看本站专题:《Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

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

人气教程排行