/en/core-libraries/global-constants-and-functions.rst

https://github.com/jamiemill/docs · ReStructuredText · 181 lines · 109 code · 72 blank · 0 comment · 0 complexity · c8de032af6627090a18d40973e27d70a MD5 · raw file

  1. Global Constants and Functions
  2. ##############################
  3. While most of your day-to-day work in CakePHP will be utilizing
  4. core classes and methods, CakePHP features a number of global
  5. convenience functions that may come in handy. Many of these
  6. functions are for use with CakePHP classes (loading model or
  7. component classes), but many others make working with arrays or
  8. strings a little easier.
  9. Well also cover some of the constants available in CakePHP
  10. applications. Using these constants will help make upgrades more
  11. smooth, but are also convenient ways to point to certain files or
  12. directories in your CakePHP application.
  13. Global Functions
  14. ================
  15. Here are CakePHP's globally available functions. Many of them are
  16. convenience wrappers for long-named PHP functions, but some of them
  17. (like ``uses()``) can be used to include code or perform other
  18. useful functions. Chances are if you're constantly wanting a
  19. function to accomplish an oft-used task, it's here.
  20. .. php:function:: \_\_(string $string_id, [$formatArgs])
  21. This function handles localization in CakePHP applications. The
  22. ``$string_id`` identifies the ID for a translation. Strings
  23. used for translations are treated as format strings for
  24. ``sprintf()``. You can supply additional arguments to replace
  25. placeholders in your string::
  26. <?php
  27. __('You have %s unread messages', $number);
  28. .. note::
  29. Check out the
  30. :doc:`/core-libraries/internationalization-and-localization`
  31. section for more information.
  32. .. php:function:: am(array $one, $two, $three...)
  33. Merges all the arrays passed as parameters and returns the merged
  34. array.
  35. .. php:function:: config()
  36. Can be used to load files from your application ``config``-folder
  37. via include\_once. Function checks for existence before include and
  38. returns boolean. Takes an optional number of arguments.
  39. Example: ``config('some_file', 'myconfig');``
  40. .. php:function:: convertSlash(string $string)
  41. Converts forward slashes to underscores and removes the first and
  42. last underscores in a string. Returns the converted string.
  43. .. php:function:: debug(mixed $var, boolean $showHtml = null, $showFrom = true)
  44. If the application's DEBUG level is non-zero, $var is printed out.
  45. If ``$showHTML`` is trueor left null, the data is rendered to be
  46. browser-friendly.
  47. If $showFrom is not set to false, the debug output will start with the line from
  48. which it was called
  49. Also see :doc:`/development/debugging`
  50. .. php:function:: env(string $key)
  51. Gets an environment variable from available sources. Used as a
  52. backup if ``$_SERVER`` or ``$_ENV`` are disabled.
  53. This function also emulates PHP\_SELF and DOCUMENT\_ROOT on
  54. unsupporting servers. In fact, it's a good idea to always use
  55. ``env()`` instead of ``$_SERVER`` or ``getenv()`` (especially if
  56. you plan to distribute the code), since it's a full emulation
  57. wrapper.
  58. .. php:function:: fileExistsInPath(string $file)
  59. Checks to make sure that the supplied file is within the current
  60. PHP include\_path. Returns a boolean result.
  61. .. php:function:: h(string $text, string $charset = null)
  62. Convenience wrapper for ``htmlspecialchars()``.
  63. .. php:function:: pr(mixed $var)
  64. Convenience wrapper for ``print_r()``, with the addition of
  65. wrapping <pre> tags around the output.
  66. .. php:function:: stripslashes_deep(array $value)
  67. Recursively strips slashes from the supplied ``$value``. Returns
  68. the modified array.
  69. Core Definition Constants
  70. =========================
  71. Most of the following constants refer to paths in your application.
  72. .. php:const:: APP
  73. root directory.
  74. .. php:const:: APP\_PATH
  75. app directory.
  76. .. php:const:: CACHE
  77. cache files directory.
  78. .. php:const:: CAKE
  79. cake directory.
  80. .. php:const:: CONTROLLER\_TESTS
  81. controller tests directory.
  82. .. php:const:: CSS
  83. CSS files directory.
  84. .. php:const:: DS
  85. Short for PHP's DIRECTORY\_SEPARATOR, which is / on Linux and \\ on windows.
  86. .. php:const:: HELPER\_TESTS
  87. helper tests directory.
  88. .. php:const:: IMAGES
  89. images directory.
  90. .. php:const:: JS
  91. JavaScript files directory (in the webroot).
  92. .. php:const:: LIB\_TESTS
  93. CakePHP Library tests directory.
  94. .. php:const:: LIBS
  95. CakePHP libs directory.
  96. .. php:const:: LOGS
  97. logs directory (in app).
  98. .. php:const:: MODEL\_TESTS
  99. model tests directory.
  100. .. php:const:: SCRIPTS
  101. Cake scripts directory.
  102. .. php:const:: TESTS
  103. tests directory (parent for the models, controllers, etc. test directories)
  104. .. php:const:: TMP
  105. tmp directory.
  106. .. php:const:: VENDORS
  107. vendors directory.
  108. .. php:const:: WWW\_ROOT
  109. full path to the webroot.