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.
_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
boolean
_loadFile( string $name , string $plugin , array $search , string $file , boolean $return )
Helper function to include single files
$name
$plugin
$search
$file
$return
mixed
_loadVendor( string $name , string $plugin , string $file , string $ext )
Helper function to load files from vendors folders
$name
$plugin
$file
$ext
boolean
_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 mixed
_packageFormat( )
Sets then returns the templates for each customizable package path
array
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
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
array
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 $name
optional null $parent
optional true $search
optional array() $file
optional null $return
optional false boolean
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
boolean
location( string $className )
Returns the package name where a class was defined to be located at
$className
string|null
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 mixed
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 array
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()
array
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
string
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
string
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
string | APPEND Append paths | 'append' |
string | PREPEND Prepend paths | 'prepend' |
string | REGISTER Register package | 'register' |
boolean | RESET Reset paths instead of merging | 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.
http://api.cakephp.org/2.7/class-App.html