Constants & Functions
While most of your day-to-day work in CakePHP will be utilizing core classes and methods, CakePHP features a number of global convenience functions that may come in handy. Many of these functions are for use with CakePHP classes (loading model or component classes), but many others make working with arrays or strings a little easier.
We'll also cover some of the constants available in CakePHP applications. Using these constants will help make upgrades more smooth, but are also convenient ways to point to certain files or directories in your CakePHP application.
Global Functions
Here are CakePHP's globally available functions. Most of them are just convenience wrappers for other CakePHP functionality, such as debugging and translating content. By default only namespaced functions are autoloaded, however you can optionally load global aliases by adding:
require CAKE . 'functions.php';To your application's config/bootstrap.php. Doing this will load global aliases for all functions listed below.
This function handles localization in CakePHP applications. The
$string_ididentifies the ID for a translation. You can supply additional arguments to replace placeholders in your string:text__('You have {0} unread messages', $number);You can also provide a name-indexed array of replacements:
text__('You have {unread} unread messages', ['unread' => $number]);NOTE
Check out the Internationalization & Localization section for more information.
Allows you to override the current domain for a single message lookup.
Useful when internationalizing a plugin:
echo __d('plugin_name', 'This is my plugin');NOTE
Make sure to use the underscored version of the plugin name here as domain.
Allows you to override the current domain for a single plural message lookup. Returns correct plural form of message identified by
$singularand$pluralfor count$countfrom domain$domain.Allows you to override the current domain for a single message lookup. It also allows you to specify a context.
The context is a unique identifier for the translations string that makes it unique within the same domain.
Allows you to override the current domain for a single plural message lookup. It also allows you to specify a context. Returns correct plural form of message identified by
$singularand$pluralfor count$countfrom domain$domain. Some languages have more than one form for plural messages dependent on the count.The context is a unique identifier for the translations string that makes it unique within the same domain.
Returns correct plural form of message identified by
$singularand$pluralfor count$count. Some languages have more than one form for plural messages dependent on the count.The context is a unique identifier for the translations string that makes it unique within the same domain.
Returns correct plural form of message identified by
$singularand$pluralfor count$countfrom domain$domain. It also allows you to specify a context. Some languages have more than one form for plural messages dependent on the count.The context is a unique identifier for the translations string that makes it unique within the same domain.
Convenience wrapper for instantiating a new
Cake\Collection\Collectionobject, wrapping the passed argument. The$itemsparameter takes either aTraversableobject or an array.If the core
$debugvariable istrue,$varis printed out. If$showHTMListrueor left asnull, the data is rendered to be browser-friendly. If$showFromis not set tofalse, the debug output will start with the line from which it was called. Also see DebuggingIt behaves like
debug(), but execution is also halted. If the core$debugvariable istrue,$varis printed. If$showHTMListrueor left asnull, the data is rendered to be browser-friendly. Also see DebuggingConvenience wrapper for
print_r(), with the addition of wrapping<pre>tags around the output.JSON pretty print convenience function, with the addition of wrapping
<pre>tags around the output.It is meant for debugging the JSON representation of objects and arrays.
Gets an environment variable from available sources. Used as a backup if
$_SERVERor$_ENVare disabled.This function also emulates
PHP_SELFandDOCUMENT_ROOTon unsupporting servers. In fact, it's a good idea to always useenv()instead of$_SERVERorgetenv()(especially if you plan to distribute the code), since it's a full emulation wrapper.Convenience wrapper for
htmlspecialchars().Splits a dot syntax plugin name into its plugin and class name. If
$namedoes not have a dot, then index 0 will benull.Commonly used like
list($plugin, $name) = pluginSplit('Users.User');Split the namespace from the classname.
Commonly used like
list($namespace, $className) = namespaceSplit('Cake\Core\App');
Core Definition Constants
Most of the following constants refer to paths in your application.
Absolute path to your application directory, including a trailing slash.
Equals
appor the name of your application directory.Path to the cache files directory. It can be shared between hosts in a multi-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 resources directory.
Path to the root directory.
Path to the tests directory.
Path to the temporary files directory.
Full path to the webroot.
Timing Definition Constants
Unix timestamp in microseconds as a float from when the application started.