Introspecting your CakePHP application with the App class
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
In 1.3 App has taken over responsibility for your application’s path configuration. You can append in new paths for CakePHP to look for classes using App::build().
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::import(), 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.
I wrote an article about it on my French blog a few weeks ago, to share a code allowing to list all the Models of an application (including in plugins).
In case it could help anyone here, I pasted the code in a bin along with a 1.2 compatible version of it (created in response to a comment on my post). One could find the snippet here: http://bin.cakephp.org/view/43202428
Pierre Martin on 4/5/10
Good tips and i started work with since pierre has published his post on his blog, now i want to find a way to able or disable plugins from the application interface, i know may be the plugin can has a components, helpers or even behaviours …
cauz i develop a cms and i want to make blog as a plugin not pre-built like croogo (i believe a page is the heart of any web application).
cherif on 11/6/10