The app class is responsible for path management, class location and class loading.
Make sure you follow the File and Class Name Conventions.
Packages
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 your 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.
Overriding classes in CakePHP
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:
To override the Dispatcher
class, create app/Lib/Routing/Dispatcher.php
To override the CakeRoute
class, create app/Lib/Routing/Route/CakeRoute.php
To override the Model
class, create app/Lib/Model/Model.php
When you load the overridden classes now, the files in app/Lib
will be loaded
instead of the built-in core ones.
Loading Vendor Files
You can use App::uses()
to load classes in vendors directories. It follows
the same conventions as loading other files:
// 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()
:
// Load the class ClassInSomePackage in
// app/Vendor/SomePackage/ClassInSomePackage.php
App::build(array('Vendor' => array(APP . 'Vendor' . DS . 'SomePackage' . DS)));
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:
App::import('Vendor', 'geshi');
Note
The geshi file must be a lower-case file name as CakePHP will not
find it otherwise.
To load app/Vendor/flickr/flickr.php:
App::import('Vendor', 'flickr', array('file' => 'flickr/flickr.php'));
To load app/Vendor/some.name.php:
App::import('Vendor', 'SomeName', array('file' => 'some.name.php'));
To load app/Vendor/services/well.named.php:
App::import(
'Vendor',
'WellNamed',
array('file' => 'services' . DS . 'well.named.php')
);
To load app/Plugin/Awesome/Vendor/services/well.named.php:
App::import(
'Vendor',
'Awesome.WellNamed',
array('file' => 'services' . DS . 'well.named.php')
);
To load app/Plugin/Awesome/Vendor/Folder/Foo.php:
App::import(
'Vendor',
'Awesome.Foo',
array('file' => 'Folder' . DS . 'Foo.php'));
It wouldn’t make a difference if your vendor files are inside your /vendors
directory. CakePHP will automatically find it.
To load vendors/vendorName/libFile.php:
App::import(
'Vendor',
'aUniqueIdentifier',
array('file' => 'vendorName' . DS . 'libFile.php')
);