Chapter 11. Cake's Global Constants And Functions

Table of Contents

11.1. Global Functions
11.2. CakePHP Core Definition Constants
11.3. CakePHP Path Constants
11.4. CakePHP Webroot Configuration Paths

Here are some globally available constants and functions that you might find useful as you build your application with Cake.

11.1. Global Functions

Here are Cake'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 wanting a nice little function to do something annoying over and over, it's here.

config( );  
  ;

Loads Cake's core configuration file. Returns true on success.

uses( $lib1,  
  $lib2...);  
string   $lib1;
string   $lib2...;

Used to load Cake's core libaries (found in cake/libs/). Supply the name of the lib filename without the '.php' extension.

uses('sanitize', 'security');
vendor( $lib1,  
  $lib2...);  
string   $lib1;
string   $lib2...;

Used to load external libraries found in the /vendors directory. Supply the name of the lib filename without the '.php' extension.

vendor('myWebService', 'nusoap');
debug( $var,  
  $showHtml = false);  
mixed   $var;
boolean   $showHtml = false;

If the application's DEBUG level is non-zero, the $var is printed out. If $showHTML is true, the data is rendered to be browser-friendly.

a( );  
  ;

Returns an array of the parameters used to call the wrapping function.

function someFunction()
{
    echo print_r(a('foo', 'bar'));
}

someFunction();

// output:

array(
    [0] => 'foo',
    [1] => 'bar'
)
aa( );  
  ;

Used to create associative arrays formed from the parameters used to call the wrapping function.

echo aa('a','b');

// output:

array(
    'a' => 'b'
) 
e( $text);  
string   $text;

Convenience wrapper for echo().

low( );  
  ;

Convenience wrapper for strtolower().

up( );  
  ;

Convenience wrapper for strtoupper().

r( $search,  
  $replace,  
  $subject);  
string   $search;
string   $replace;
string   $subject;

Convenience wrapper for str_replace().

pr( $data);  
mixed   $data;

Convenience function equivalent to:

echo "<pre>" . print_r($data) . "</pre>";

Only prints out information if DEBUG is non-zero.

am( $array1,  
  $array2...);  
array   $array1;
array   $array2...;

Merges and returns the arrays supplied in the parameters.

env( $key);  
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.

cache( $path,  
  $expires,  
  $target = 'cache');  
string   $path;
string   $expires;
string   $target = 'cache';

Writes the data in $data to the path in /app/tmp specified by $path as a cache. The expiration time specified by $expires must be a valid strtotime() string. The $target of the cached data can either be 'cache' or 'public'.

clearCache( $search,  
  $path = 'views',  
  $ext);  
string   $search;
string   $path = 'views';
string   $ext;

Used to delete files in the cache directories, or clear contents of cache directories.

If $search is a string, matching cache directory or file names will be removed from the cache. The $search parameter can also be passed as an array of names of files/directories to be cleared. If empty, all files in /app/tmp/cache/views will be cleared.

The $path parameter can be used to specify which directory inside of /tmp/cache is to be cleared. Defaults to 'views'.

The $ext param is used to specify files with a certain file extention you wish to clear.

stripslashes_deep( $array);  
array   $array;

Recursively strips slashes from all values in an array.

countdim( $array);  
array   $array;

Returns the number of dimensions in the supplied array.

fileExistsInPath( $file);  
string   $file;

Searches the current include path for a given filename. Returns the path to that file if found, false if not found.

convertSlash( $string);  
string   $string;

Converts forward slashes to underscores and removes first and last underscores in a string.