This document is for a version of CakePHP that is no longer supported. Please upgrade to a newer release!
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. Many of them are
convenience wrappers for long-named PHP functions, but some of them
(like 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.
__(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 Internationalization & Localization section for more information.
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'
)
aa(string $one, $two, $three...)
Used to create associative arrays formed from the parameters used to call the wrapping function.
print_r(aa('a','b'));
// output:
array(
'a' => 'b'
)
am(array $one, $two, $three...)
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 existance before include and returns
boolean. Takes an optional number of arguments.
Example: config('some_file', 'myconfig');
convertSlash(string $string)
Converts forward slashes to underscores and removes the first and last underscores in a string. Returns the converted string.
countdim(array $array)
Returns the number of dimensions in the supplied array.
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.
e(mixed $data)
Convenience wrapper for echo()
.
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.
fileExistsInPath(string $file)
Checks to make sure that the supplied file is within the current PHP include_path. Returns a boolean result.
h(string $text, string $charset = null)
Convenience wrapper for htmlspecialchars()
.
ife($condition, $ifNotEmpty, $ifEmpty)
Used for ternary-like operations. If the $condition
is non-empty,
$ifNotEmpty
is returned, else $ifEmpty
is returned.
low(string $string)
Convenience wrapper for strtolower()
.
paths()
Get CakePHP basic paths as an indexed array. Resulting array will contain array of paths indexed by: Models, Behaviors, Controllers, Components, and Helpers.
This has been Deprecated and is no longer available in RC2. Use Configure::corePaths(); instead.
pr(mixed $var)
Convenience wrapper for print_r()
, with the addition of wrapping
<pre> tags around the output.
r(string $search, string $replace, string $subject)
Convenience wrapper for str_replace()
.
stripslashes_deep(array $value)
Recursively strips slashes from the supplied $value
. Returns the
modified array.
up(string $string)
Convenience wrapper for strtoupper()
.
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.
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.
DS
Short for PHP’s DIRECTORY_SEPARATOR, which is / on Linux and \ on windows.
ELEMENTS
elements directory.
HELPER_TESTS
helper tests directory.
HELPERS
helpers directory.
IMAGES
images 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.