3.14 Global Constants and 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.
3.14.1 Global Functions
Here are CakePHP's globally available functions. Many of them are convenience wrappers for long-named PHP functions, but some of them (like vendor() and uses()) can be used to include code or perform other useful functions. Chances are if you're constantly wanting a function to accomplish an oft-used task, it's here.
3.14.1.1 __
__(string $string_id, boolean $return = false)
This function handles localization in CakePHP applications. The $string_id identifies the ID for a translation, and the second parameter allows you to have the function automatically echo the string (the default behavior), or return it for further processing (pass a boolean true to enable this behavior).
Check out the Localization & Internationalization section for more information.
3.14.1.2 a
a(mixed $one, $two, $three...)
Returns an array of the parameters used to call the wrapping function.
print_r(a('foo', 'bar'));
// output:
array(
[0] => 'foo',
[1] => 'bar'
) print_r(a('foo', 'bar'));// output:array([0] => 'foo',[1] => 'bar')
3.14.1.3 aa
aa(array $one, $two, $three...)
Used to create associative arrays formed from the parameters used to call the wrapping function.
echo aa('a','b');
// output:
array(
'a' => 'b'
) echo aa('a','b');// output:array('a' => 'b')
3.14.1.4 am
am(array $one, $two, $three...)
Merges all the arrays passed as parameters and returns the merged array.
3.14.1.5 convertSlash
convertSlash(string $string)
Converts forward slashes to underscores and removes the first and last underscores in a string. Returns the converted string.
3.14.1.6 countdim
countdim(array $array)
Returns the number of dimensions in the supplied array.
3.14.1.7 debug
debug(mixed $var, boolean $showHtml = false)
If the application's DEBUG level is non-zero, $var is printed out. If $showHTML is true, the data is rendered to be browser-friendly.
3.14.1.8 e
e(mixed $data)
Convenience wrapper for echo().
3.14.1.9 env
env(string $key)
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.
3.14.1.10 fileExistsInPath
fileExistsInPath(string $file)
Checks to make sure that the supplied file is within the current PHP include_path. Returns a boolean result.
3.14.1.11 h
h(string $text, string $charset)
Convenience wrapper for htmlspecialchars().
3.14.1.12 ife
ife($condition, $ifNotEmpty, $ifEmpty)
Used for ternary-like operations. If the $condition is non-empty, $ifNotEmpty is returned, else $ifEmpty is returned.
3.14.1.13 low
low(string $string)
Convenience wrapper for strtolower().
3.14.1.14 paths
paths()
Get CakePHP basic paths as an indexed array. Resulting array will contain array of paths indexed by: Models, Behaviors, Controllers, Components, and Helpers.
3.14.1.15 pr
pr(mixed $var)
Convenience wrapper for print_r(), with the addition of wrapping <pre> tags around the output.
3.14.1.16 r
r(string $search, string $replace, string $subject)
Convenience wrapper for str_replace().
3.14.1.17 stripslashes_deep
stripslashes_deep(array $value)
Recursively strips slashes from the supplied $value. Returns the modified array.
3.14.1.18 up
up(string $string)
Convenience wrapper for strtoupper().
3.14.1.19 uses
uses(string $lib1, $lib2, $lib3...)
Used to load CakePHP's core libraries (found in cake/libs/). Supply the name of the library's file name without the '.php' extension.
3.14.2 Core Definition Constants
| constant | Absolute path to the application’s... |
|---|---|
APP |
root directory. |
APP_PATH |
app directory. |
CACHE |
cache files directory. |
CAKE |
cake directory. |
COMPONENTS |
components directory. |
CONFIGS |
configuration files directory. |
CONTROLLER_TESTS |
controller tests directory. |
CONTROLLERS |
controllers directory. |
CSS |
CSS files directory. |
ELEMENTS |
elements directory. |
HELPER_TESTS |
helper tests directory. |
HELPERS |
helpers directory. |
INFLECTIONS |
inflections directory (usually inside the configuration directory). |
JS |
JavaScript files directory (in the webroot). |
LAYOUTS |
layouts directory. |
LIB_TESTS |
CakePHP Library tests directory. |
LIBS |
CakePHP libs directory. |
LOGS |
logs directory (in app). |
MODEL_TESTS |
model tests directory. |
MODELS |
models directory. |
SCRIPTS |
Cake scripts directory. |
TESTS |
tests directory (parent for the models, controllers, etc. test directories) |
TMP |
tmp directory. |
VENDORS |
vendors directory. |
VIEWS |
views directory. |
WWW_ROOT |
full path to the webroot. |
