The app class is responsible for path management, class location and class loading. Make sure you follow the File and Classname Conventions.
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 with your own compatible implementation. If you wish to use you own class instead of the classes the framework provides, just add the class to your libs folder emulating 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/Lib/Network/Http/HttpSocket.php
Once you’ve done this App will load your override file instead of the file inside CakePHP.
Return type: | void |
---|
Classes are lazily loaded in CakePHP, however before the autoloader can find your classes you need to tell App, where it can find the files. By telling App which package a class can be found in, it can properly locate the file and load it the first time a class is used.
Some examples for common types of classes are:
So basically the second param should simply match the folder path of the class file in core or app.
Note
Loading vendors usually means you are loading packages that do not follow conventions. For most vendor packages using App::import() is recommended.
Loading classes in plugins works much the same as loading app and core classes except you must specify the plugin you are loading from:
<?php
// Load the class Comment in app/Plugin/PluginName/Model/Comment.php
App::uses('Comment', 'PluginName.Model');
// Load the class CommentComponent in app/Plugin/PluginName/Controller/Component/CommentComponent.php
App::uses('CommentComponent', 'PluginName.Controller/Component');
Return type: | array |
---|
Used to read information stored path:
<?php
// return the model paths in your application
App::path('Model');
This can be done for all packages that are apart of your application. You can also fetch paths for a plugin:
<?php
// return the component paths in DebugKit
App::path('Component', 'DebugKit');
Return type: | array |
---|
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()
Return type: | array |
---|
Used for finding the path to a package inside CakePHP:
<?php
// Get the path to Cache engines.
App::core('Cache/Engine');
Return type: | string |
---|
Returns the package name where a class was defined to be located at.
Return type: | void |
---|
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.
Adding additional controller paths for example would alter where CakePHP looks for controllers. This allows you to split your application up across the filesystem.
Usage:
<?php
//will setup a new search path for the Model package
App::build(array('Model' => array('/a/full/path/to/models/')));
//will setup the path as the only valid path for searching models
App::build(array('Model' => array('/path/to/models/')), App::RESET);
//will setup multiple search paths for helpers
App::build(array('View/Helper' => array('/path/to/helpers/', '/another/path/')));
If reset is set to true, all loaded plugins will be forgotten and they will be needed to be loaded again.
Examples:
<?php
App::build(array('controllers' => array('/full/path/to/controllers')));
//becomes
App::build(array('Controller' => array('/full/path/to/Controller')));
App::build(array('helpers' => array('/full/path/to/views/helpers')));
//becomes
App::build(array('View/Helper' => array('/full/path/to/View/Helper')));
Changed in version 2.0: App::build() will not merge app paths with core paths anymore.
App::build() can be used to add new package locations. This is useful when you want to add new top level packages or, sub-packages to your application:
<?php
App::build(array(
'Service' => array('%s' . 'Service' . DS)
), App::REGISTER);
The %s in newly registered packages will be replaced with the APP path. You must include a trailing / in registered packages. Once packages are registered, you can use App::build() to append/prepend/reset paths like any other package.
Changed in version 2.1: Registering packages was added in 2.1
Return type: | mixed Returns an array of objects of the given type or false if incorrect. |
---|
You can find out which objects App knows about using App::objects('Controller') for example to find which application controllers App knows about.
Example usage:
<?php
//returns array('DebugKit', 'Blog', 'User');
App::objects('plugin');
//returns array('PagesController', 'BlogController');
App::objects('Controller');
You can also search only within a plugin’s objects by using the plugin dot syntax.:
<?php
// returns array('MyPluginPost', 'MyPluginComment');
App::objects('MyPlugin.Model');
Changed in version 2.0.
Return type: | string |
---|
Plugins can be located with App as well. Using App::pluginPath('DebugKit'); for example, will give you the full path to the DebugKit plugin:
<?php
$path = App::pluginPath('DebugKit');
Return type: | string |
---|
Themes can be found App::themePath('purple');, would give the full path to the purple theme.
Return type: | boolean |
---|
At first glance App::import seems complex, however in most use cases only 2 arguments are required.
Note
This method is equivalent to require‘ing the file. It is important to realize that the class subsequently needs to be initialized.
<?php
// The same as require('Controller/UsersController.php');
App::import('Controller', 'Users');
// We need to load the class
$Users = new UsersController();
// If we want the model associations, components, etc to be loaded
$Users->constructClasses();
All classes that were loaded in the past using App::import(‘Core’, $class) will need to be loaded using App::uses() referring to the correct package. This change has provided large performance gains to the framework.
Changed in version 2.0.
You can override almost every class in the framework, exceptions are the App and Configure classes. whenever you like to perform such overriding, just add your class to your app/Lib folder mimicking the internal structure of the framework. Some examples to follow
When you load the replaced files, the app/Lib files will be loaded instead of the built-in core classes.
You can use App::uses() to load classes in vendors directories. It follows the same conventions as loading other files:
<?php
// Load the class Geshi in app/Vendor/Geshi.php
App::uses('Geshi', 'Vendor');
To load classes in subdirectories, you’ll need to add those paths with App::build():
<?php
// Load the class ClassInSomePackage in app/Vendor/SomePackage/ClassInSomePackage.php
App::build(array('Vendor' => array(APP . 'Vendor' . DS . 'SomePackage')));
App::uses('ClassInSomePackage', 'Vendor');
Your vendor files may not follow conventions, have a class that differs from the file name or does not contain classes. You can load those files using App::import(). The following examples illustrate how to load vendor files from a number of path structures. These vendor files could be located in any of the vendor folders.
To load app/Vendor/geshi.php:
<?php
App::import('Vendor', 'geshi');
Note
The geshi file must be a lower-case file name as Cake will not find it otherwise.
To load app/Vendor/flickr/flickr.php:
<?php
App::import('Vendor', 'flickr/flickr');
To load app/Vendor/some.name.php:
<?php
App::import('Vendor', 'SomeName', array('file' => 'some.name.php'));
To load app/Vendor/services/well.named.php:
<?php
App::import('Vendor', 'WellNamed', array('file' => 'services' . DS . 'well.named.php'));
It wouldn’t make a difference if your vendor files are inside your /vendors directory. Cake will automatically find it.
To load vendors/vendorName/libFile.php:
<?php
App::import('Vendor', 'aUniqueIdentifier', array('file' => 'vendorName' . DS . 'libFile.php'));
Return type: | void |
---|
Initializes the cache for App, registers a shutdown function.
Return type: | boolean |
---|
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
Return type: | void |
---|
Object destructor. Writes cache file if changes have been made to the $_map.