Constants & Functions
While most of your day-to-day work in CakePHP will be utilizing core classes andmethods, CakePHP features a number of global convenience functions that may comein handy. Many of these functions are for use with CakePHP classes (loadingmodel or component classes), but many others make working with arrays orstrings a little easier.
We’ll also cover some of the constants available in CakePHP applications. Usingthese constants will help make upgrades more smooth, but are also convenientways to point to certain files or directories in your CakePHP application.
Global Functions
Here are CakePHP’s globally available functions. Most of them are justconvenience wrappers for other CakePHP functionality, such as debugging andtranslating content.
(string $string_id[, $formatArgs])[](#)
- This function handles localization in CakePHP applications. The
$string_id
identifies the ID for a translation. You can supplyadditional arguments to replace placeholders in your string:
- __('You have {0} unread messages', $number);
You can also provide a name-indexed array of replacements:
- __('You have {unread} unread messages', ['unread' => $number]);
Note
Check out theInternationalization & Localization section formore information.
d
(string $domain, string $msg, mixed $args = null)[](#d)- Allows you to override the current domain for a single message lookup.
Useful when internationalizing a plugin:echo __d('PluginName', 'This is my plugin');
dn
(string $domain, string $singular, string $plural, integer $count, mixed $args = null)[](#dn)Allows you to override the current domain for a single plural messagelookup. Returns correct plural form of message identified by
$singular
and$plural
for count$count
from domain$domain
.dx
(string $domain, string $context, string $msg, mixed $args = null)[](#dx)- Allows you to override the current domain for a single message lookup. Italso allows you to specify a context.
The context is a unique identifier for the translations string that makes itunique within the same domain.
dxn
(string $domain, string $context, string $singular, string $plural, integer $count, mixed $args = null)[](#dxn)- Allows you to override the current domain for a single plural messagelookup. It also allows you to specify a context. Returns correct pluralform of message identified by
$singular
and$plural
for count$count
from domain$domain
. Some languages have more than one formfor plural messages dependent on the count.
The context is a unique identifier for the translations string that makes itunique within the same domain.
n
(string $singular, string $plural, integer $count, mixed $args = null)[](#n)Returns correct plural form of message identified by
$singular
and$plural
for count$count
. Some languages have more than one form forplural messages dependent on the count.x
(string $context, string $msg, mixed $args = null)[](#x)The context is a unique identifier for the translations string that makes itunique within the same domain.
xn
(string $context, string $singular, string $plural, integer $count, mixed $args = null)[](#xn)- Returns correct plural form of message identified by
$singular
and$plural
for count$count
from domain$domain
. It also allows youto specify a context. Some languages have more than one form for pluralmessages dependent on the count.
The context is a unique identifier for the translations string that makes itunique within the same domain.
collection
(mixed $items)Convenience wrapper for instantiating a new
Cake\Collection\Collection
object, wrapping the passed argument. The$items
parameter takes eitheraTraversable
object or an array.debug
(mixed $var, boolean $showHtml = null, $showFrom = true)
Changed in version 3.3.0: Calling this method will return passed $var
, so that you can, for instance,place it in return statements.
If the core $debug
variable is true
, $var
is printed out.If $showHTML
is true
or left as null
, the data is rendered to bebrowser-friendly. If $showFrom
is not set to false
, the debug outputwill start with the line from which it was called. Also seeDebugging
dd
(mixed $var, boolean $showHtml = null)It behaves like
debug()
, but execution is also halted.If the core$debug
variable istrue
,$var
is printed.If$showHTML
istrue
or left asnull
, the data is rendered to bebrowser-friendly. Also see Debugging
Changed in version 3.3.0: Calling this method will return passed $var
, so that you can, for instance,place it in return statements.
Convenience wrapper for print_r()
, with the addition ofwrapping <pre>
tags around the output.
Changed in version 3.3.0: Calling this method will return passed $var
, so that you can, for instance,place it in return statements.
JSON pretty print convenience function, with the addition ofwrapping <pre>
tags around the output.
It is meant for debugging the JSON representation of objects and arrays.
Changed in version 3.1.1: The $default
parameter has been added.
Gets an environment variable from available sources. Used as a backup if$_SERVER
or $_ENV
are disabled.
This function also emulates PHP_SELF
and DOCUMENT_ROOT
onunsupporting servers. In fact, it’s a good idea to always use env()
instead of $_SERVER
or getenv()
(especially if you plan todistribute the code), since it’s a full emulation wrapper.
h
(string $text, boolean $double = true, string $charset = null)Convenience wrapper for
htmlspecialchars()
.pluginSplit
(string $name, boolean $dotAppend = false, string $plugin = null)- Splits a dot syntax plugin name into its plugin and class name. If
$name
does not have a dot, then index 0 will benull
.
Commonly used like list($plugin, $name) = pluginSplit('Users.User');
Commonly used like list($namespace, $className) = namespaceSplit('Cake\Core\App');
Core Definition Constants
Most of the following constants refer to paths in your application.
- constant
APP
Absolute path to your application directory, including a trailing slash.
Equals
app
or the name of your application directory.Path to the cache files directory. It can be shared between hosts in amulti-server setup.
Path to the cake directory.
Path to the root lib directory.
Path to the config directory.
Path to the CakePHP directory with ending directory slash.
Short for PHP’s
DIRECTORY_SEPARATOR
, which is/
on Linux and\
on Windows.Path to the logs directory.
Path to the root directory.
Path to the tests directory.
Path to the temporary files directory.
- Full path to the webroot.