Normally when people think of the App class, they think of a file loader. However, App is also able to introspect your application and provide information about the resources it contains.
Configuring additional paths
App has taken over responsibility for your application’s path configuration. You can append in new paths for CakePHP to look for classes using
You can append additional paths for pretty much any object that could be loaded with
App::import(). After using
build(), any subsequent calls to
import() will also scan these paths. Just be sure to make sure the paths have a trailing
/ otherwise they will not work. You can also fully replace all the paths App knows about by passing in
true as the second argument. This will force App to discard all its current paths, and only use the ones provided. This is a huge boon when writing tests, as controlling your paths makes testing more predictable.
Finding out what things are installed
You can also use
App to find out information about your application at runtime. You use
App::objects() to find out what things are available. For example
App::objects('plugin'); will tell you which plugins are available in all of the configured plugin paths. The same can be done for almost any type of object in a CakePHP application. It should be noted though that
objects() caches the results in memory. So multiple calls do not continuously scan the disk. If you’d like a fresh read on things you can use
App::objects('plugin', null, false); . Doing this will clear and rebuild the cache for that type of object, and give you the most up to date information. It should be noted though, that unlike
App::objects() will not recurse into sub-directories. It will only scan exactly the directory you ask it to/its configured for.
App also provides a way to find out where on the filesystem a certain plugin is. Using
App::pluginPath($pluginName); will return you the full file path to a particular plugin. This is great for finding out information about plugins such as the controllers or models they contain.
- $path = App::pluginPath('DebugKit');
- $helpers = App::objects('helper', $path);
This allows you to find out additional information about a plugin without having to manually traverse the file system. I hope this explains some of the additional features that App has picked up in 1.3 and how you might be able to use them in your applications.