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.

__(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.

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'
)
  1. print_r(a('foo', 'bar'));
  2. // output:
  3. array(
  4. [0] => 'foo',
  5. [1] => 'bar'
  6. )

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'
)
  1. echo aa('a','b');
  2. // output:
  3. array(
  4. 'a' => 'b'
  5. )

am(array $one, $two, $three...)

Merges all the arrays passed as parameters and returns the merged array.

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)

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.

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 libaries (found in cake/libs/). Supply the name of the lib filename 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.

3.14.3 Path Constants

Default Content