App is responsible for path management, class location and class loading.
You can add paths to the search indexes App uses to find classes using App::build()
. Adding additional controller paths for example would alter where CakePHP looks for controllers. This allows you to split your application up across the filesystem.
CakePHP is organized around the idea of packages, each class belongs to a package or folder where other classes reside. You can configure each package location in your application using App::build('APackage/SubPackage', $paths)
to inform the framework where should each class be loaded. Almost every class in the CakePHP framework can be swapped by your own compatible implementation. If you wish to use your own class instead of the classes the framework provides, just add the class to your libs folder mocking the directory location of where CakePHP expects to find it.
For instance if you'd like to use your own HttpSocket class, put it under
app/Network/Http/HttpSocket.php
You can inspect the currently loaded paths using App::path('Controller')
for example to see loaded controller paths.
It is also possible to inspect paths for plugin classes, for instance, to see a plugin's helpers you would call App::path('View/Helper', 'MyPlugin')
Plugins and Themes can be located with App as well. Using App::pluginPath('DebugKit') for example, will give you the full path to the DebugKit plugin. App::themePath('purple'), would give the full path to the purple
theme.
You can find out which objects App knows about using App::objects('Controller') for example to find which application controllers App knows about.
string
'append'
string
'prepend'
string
'register'
boolean
true
boolean
array
array
boolean
array
array
array
boolean
Indicates the the Application is in the bootstrapping process. Used to better cache loaded classes while the cache libraries have not been yet initialized
array
boolean
array
array
Helper function to include classes This is a compatibility wrapper around using App::uses() and automatic class loading
Sets up each package location on the file system. You can configure multiple search paths for each package, those will be used to look for files one folder at a time in the specified order All paths should be terminated with a Directory separator
Finds classes based on $name or specific file(s) to search. Calling App::import() will not construct any classes contained in the files. It will only find and require() the file.
Increases the PHP "memory_limit" ini setting by the specified amount in kilobytes
Method to handle the automatic class loading. It will look for each class' package defined using App::uses() and with this information it will resolve the package name to a full path to load the class from. File name for each class should follow the class name. For instance, if a class is name MyCustomClass
the file name should be MyCustomClass.php
Get all the currently loaded paths from App. Useful for inspecting or storing all paths App knows about. For a paths to a specific package use App::path()
Declares a package for a class. This package location will be used by the automatic class loader if the class is tried to be used
_checkFatalError( )
Check if a fatal error happened and trigger the configured handler if configured
_loadClass( string $name , string $plugin , string $type , string $originalType , boolean $parent )
Helper function to include classes This is a compatibility wrapper around using App::uses() and automatic class loading
$name
$plugin
$type
$originalType
$parent
_loadFile( string $name , string $plugin , array $search , string $file , boolean $return )
Helper function to include single files
$name
$plugin
$search
$file
$return
_loadVendor( string $name , string $plugin , string $file , string $ext )
Helper function to load files from vendors folders
$name
$plugin
$file
$ext
_map( string $file , string $name , string $plugin null )
Maps the $name to the $file.
$file
$name
$plugin
optional null _mapped( string $name , string $plugin null )
Returns a file's complete path.
$name
$plugin
optional null _packageFormat( )
Sets then returns the templates for each customizable package path
build( array $paths array() , boolean|string $mode App::PREPEND )
Sets up each package location on the file system. You can configure multiple search paths for each package, those will be used to look for files one folder at a time in the specified order All paths should be terminated with a Directory separator
Usage:
App::build(array('Model' => array('/a/full/path/to/models/'))); will setup a new search path for the Model package
App::build(array('Model' => array('/path/to/models/')), App::RESET); will setup the path as the only valid path for searching models
App::build(array('View/Helper' => array('/path/to/helpers/', '/another/path/'))); will setup multiple search paths for helpers
App::build(array('Service' => array('%s' . 'Service' . DS)), App::REGISTER); will register new package 'Service'
If reset is set to true, all loaded plugins will be forgotten and they will be needed to be loaded again.
$paths
optional array() $mode
optional App::PREPEND App::RESET will set paths, App::APPEND with append paths, App::PREPEND will prepend paths (default) App::REGISTER will register new packages and their paths, %s in path will be replaced by APP path
core( string $type )
Returns the full path to a package inside the CakePHP core
Usage:
App::core('Cache/Engine'); will return the full path to the cache engines package
$type
import( string|array $type null , string $name null , boolean|array $parent true , array $search array() , string $file null , boolean $return false )
Finds classes based on $name or specific file(s) to search. Calling App::import() will not construct any classes contained in the files. It will only find and require() the file.
$type
optional null The type of Class if passed as a string, or all params can be passed as a single array to $type.
$name
optional null $parent
optional true boolean true if Class Parent should be searched, accepts key => value array('parent' => $parent, 'file' => $file, 'search' => $search, 'ext' => '$ext'); $ext allows setting the extension of the file name based on Inflector::underscore($name) . ".$ext";
$search
optional array() $file
optional null $return
optional false Return the loaded file, the file must have a return statement in it to work: return $variable;
increaseMemoryLimit( string $additionalKb )
Increases the PHP "memory_limit" ini setting by the specified amount in kilobytes
$additionalKb
load( string $className )
Method to handle the automatic class loading. It will look for each class' package defined using App::uses() and with this information it will resolve the package name to a full path to load the class from. File name for each class should follow the class name. For instance, if a class is name MyCustomClass
the file name should be MyCustomClass.php
$className
location( string $className )
Returns the package name where a class was defined to be located at
$className
objects( string $type , string|array $path null , boolean $cache true )
Returns an array of objects of the given type.
Example usage:
App::objects('plugin');
returns array('DebugKit', 'Blog', 'User');
App::objects('Controller');
returns array('PagesController', 'BlogController');
You can also search only within a plugin's objects by using the plugin dot syntax.
App::objects('MyPlugin.Model');
returns array('MyPluginPost', 'MyPluginComment');
When scanning directories, files and directories beginning with .
will be excluded as these are commonly used by version control systems.
$type
$path
optional null $cache
optional true path( string $type , string $plugin null )
Used to read information stored path
Usage:
App::path('Model'); will return all paths for models
App::path('Model/Datasource', 'MyPlugin'); will return the path for datasources under the 'MyPlugin' plugin
$type
$plugin
optional null paths( )
Get all the currently loaded paths from App. Useful for inspecting or storing all paths App knows about. For a paths to a specific package use App::path()
pluginPath( string $plugin )
Gets the path that a plugin is on. Searches through the defined plugin paths.
Usage:
App::pluginPath('MyPlugin'); will return the full path to 'MyPlugin' plugin'
CakePlugin::path()
instead.$plugin
shutdown( )
Object destructor.
Writes cache file if changes have been made to the $_map. Also, check if a fatal error happened and call the handler.
themePath( string $theme )
Finds the path that a theme is on. Searches through the defined theme paths.
Usage:
App::themePath('MyTheme'); will return the full path to the 'MyTheme' theme
$theme
uses( string $className , string $location )
Declares a package for a class. This package location will be used by the automatic class loader if the class is tried to be used
Usage:
App::uses('MyCustomController', 'Controller');
will setup the class to be found under Controller package
App::uses('MyHelper', 'MyPlugin.View/Helper');
will setup the helper class to be found in plugin's helper package
$className
$location
protected static boolean
Indicates whether the class cache should be stored again because of an addition to it
false
protected static boolean
Indicates whether the object cache should be stored again because of an addition to it
false
protected static array
Holds the templates for each customizable package path in the application
array()
public static boolean
Indicates the the Application is in the bootstrapping process. Used to better cache loaded classes while the cache libraries have not been yet initialized
false
public static array
Maps an old style CakePHP class type to the corresponding package
array( 'models' => 'Model', 'behaviors' => 'Model/Behavior', 'datasources' => 'Model/Datasource', 'controllers' => 'Controller', 'components' => 'Controller/Component', 'views' => 'View', 'helpers' => 'View/Helper', 'shells' => 'Console/Command', 'libs' => 'Lib', 'vendors' => 'Vendor', 'plugins' => 'Plugin', 'locales' => 'Locale' )
public static array
List of object types and their properties
array( 'class' => array('extends' => null, 'core' => true), 'file' => array('extends' => null, 'core' => true), 'model' => array('extends' => 'AppModel', 'core' => false), 'behavior' => array('suffix' => 'Behavior', 'extends' => 'Model/ModelBehavior', 'core' => true), 'controller' => array('suffix' => 'Controller', 'extends' => 'AppController', 'core' => true), 'component' => array('suffix' => 'Component', 'extends' => null, 'core' => true), 'lib' => array('extends' => null, 'core' => true), 'view' => array('suffix' => 'View', 'extends' => null, 'core' => true), 'helper' => array('suffix' => 'Helper', 'extends' => 'AppHelper', 'core' => true), 'vendor' => array('extends' => null, 'core' => true), 'shell' => array('suffix' => 'Shell', 'extends' => 'AppShell', 'core' => true), 'plugin' => array('extends' => null, 'core' => true) )
© 2005–2016 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/2.9/class-App.html