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.
Here are CakePHP’s globally available functions. Most of them are just convenience wrappers for other CakePHP functionality, such as debugging and translating content.
This function handles localization in CakePHP applications. The $string_id identifies the ID for a translation. Strings used for translations are treated as format strings for sprintf(). You can supply additional arguments to replace placeholders in your string:
__('You have %s unread messages', $number);
Note
Check out the Internationalization & Localization section for more information.
Note that the category must be specified with a numeric value, instead of the constant name. The values are:
Allows you to override the current domain for a single message lookup.
Useful when internationalizing a plugin: echo __d('PluginName', 'This is my plugin');
Allows you to override the current domain for a single message lookup. It also allows you to specify a category.
Note that the category must be specified with a numeric value, instead of the constant name. The values are:
Allows you to override the current domain for a single plural message lookup. It also allows you to specify a category. Returns correct plural form of message identified by $singular and $plural for count $count from domain $domain.
Note that the category must be specified with a numeric value, instead of the constant name. The values are:
Allows you to override the current domain for a single plural message lookup. Returns correct plural form of message identified by $singular and $plural for count $count from domain $domain.
Returns correct plural form of message identified by $singular and $plural for count $count. Some languages have more than one form for plural messages dependent on the count.
Merges all the arrays passed as parameters and returns the merged array.
Can be used to load files from your application config-folder via include_once. Function checks for existence before include and returns boolean. Takes an optional number of arguments.
Example: config('some_file', 'myconfig');
Converts forward slashes to underscores and removes the first and last underscores in a string. Returns the converted string.
If the application’s DEBUG level is non-zero, $var is printed out. If $showHTML is true or left as null, the data is rendered to be browser-friendly. If $showFrom is not set to false, the debug output will start with the line from which it was called Also see Debugging
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 on unsupporting servers. In fact, it’s a good idea to always use env() instead of $_SERVER or getenv() (especially if you plan to distribute the code), since it’s a full emulation wrapper.
Checks to make sure that the supplied file is within the current PHP include_path. Returns a boolean result.
Convenience wrapper for htmlspecialchars().
Shortcut to Log::write().
Splits a dot syntax plugin name into its plugin and classname. If $name does not have a dot, then index 0 will be null.
Commonly used like list($plugin, $name) = pluginSplit('Users.User');
Convenience wrapper for print_r(), with the addition of wrapping <pre> tags around the output.
Sorts given $array by key $sortby.
Recursively strips slashes from the supplied $value. Returns the modified array.
Most of the following constants refer to paths in your application.
Path to the application’s directory.
Equals app or the name of your application directory.
Path to the application’s Lib 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 root directory with ending directory slash.
Path to the public CSS directory.
Web path to the CSS files directory.
Short for PHP’s DIRECTORY_SEPARATOR, which is / on Linux and \ on windows.
Full url prefix. Such as https://example.com
Path to the public images directory.
Web path to the public images directory.
Path to the public JavaScript directory.
Web path to the js files directory.
Path to the logs directory.
Path to the root directory.
Path to the tests directory.
Path to the temporary files directory.
Path to the vendors directory.
Equals webroot or the name of your webroot directory.
Full path to the webroot.
Unix timestamp in microseconds as a float from when the application started.
Equals 1
Equals 60
Equals 3600
Equals 86400
Equals 604800
Equals 2592000
Equals 31536000