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().

Show Plain Text
  1. App::build(array(
  2.     'plugins' => array('/Users/markstory/cake_plugins/)
  3. ));

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.

Show Plain Text
  1. $path = App::pluginPath('DebugKit');
  2. $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.

Comments

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 5/4/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 6/11/10

I must be doing something wrong because I cant seem to get this to work properly. I have my cake core files stored in a central location and all my apps reference it. I’d like to have common models, components, etc, stored there too.

I have updated my bootstrap.ctp file with the following:

App::build(array( ‘plugins’ => array(’/app/plugins/’), ‘models’ => array(’/app/models/’), ‘views’ => array(’/app/views/’), ‘controllers’ => array(’/app/controllers/’), ‘datasources’ => array(’/app/datasources/’), ‘behaviors’ => array(’/app/behaviors/’), ‘components’ => array(’/app/components/’, CAKE_CORE_INCLUDE_PATH . DS . ‘app’. DS .‘components’ . DS), ‘helpers’ => array(’/app/helpers/’), ‘vendors’ => array(’/app/vendors/’), ‘shells’ => array(’/app/shells/’), ‘locales’ => array(’/app/locale/’) ));

var_dump(CAKE_CORE_INCLUDE_PATH . DS . ‘app’. DS .‘components’ . DS);

When I add my ‘test’ component to the components array in my AppController I get an error stating that javascript is an undefined variable. This usually means that a component cant be found or has a problem. (even though Javascript is a helper).

public $helpers = array(‘Formatters’, ‘Html’, ‘Form’, ‘Javascript’, ‘Session’); public $components = array(‘RequestHandler’, ‘Session’, ‘SoapAuth’, ‘Hello’);

I could just put my components under:
C:\vhosts\_libraries\cake_1.3.7\cake\libs\controller\components
but I’m not sure thats the best strategy.

Where am I going wrong with this? Any help or advice would be appreciated. Thanks.

Steven Wright on 3/17/11

Update. Sorry I posted the wrong path previously. Here is the update although it gives the same result.

App::build(array( ‘plugins’ => array(’/app/plugins/’), ‘models’ => array(’/app/models/’), ‘views’ => array(’/app/views/’), ‘controllers’ => array(’/app/controllers/’), ‘datasources’ => array(’/app/datasources/’), ‘behaviors’ => array(’/app/behaviors/’), ‘components’ => array(’/app/components/’, CAKE_CORE_INCLUDE_PATH . DS . ‘app’. DS .‘controllers’.DS.‘components’ . DS), ‘helpers’ => array(’/app/helpers/’), ‘vendors’ => array(’/app/vendors/’), ‘shells’ => array(’/app/shells/’), ‘locales’ => array(’/app/locale/’) ));

var_dump(CAKE_CORE_INCLUDE_PATH . DS . ‘app’. DS .‘controllers’.DS.‘components’ . DS);

Steven Wright on 3/17/11

Hello There. I found your blog using msn. This is a really well written article.

I’ll make sure to bookmark it and return to read more of your useful info. Thanks for the post. I will certainly return.

file sync software real time on 9/14/12

Awesome blog you have here but I was curious about if you knew
of any forums that cover the same topics discussed in this article?
I’d really like to be a part of online community where I can get responses from other knowledgeable people that share the same interest. If you have any recommendations, please let me know. Thanks a lot!

online slotmachines on 2/20/13

Hello to every body, it’s my first pay a visit of this weblog; this web site contains remarkable and genuinely good material for visitors.

Rickie on 4/12/13

The pots nest together for quick and easy storage, saving valuable cabinet space.
The Ridge-It is a permanent fall protection roof anchor designed to install easily and discreetly with the option to provide
an access point on each side of the roof.

marylandbuildingcorp.com on 7/2/13

It’s a pity you don’t have a donate button! I’d certainly donate to this brilliant blog! I suppose for now i’ll settle for bookmarking and adding your RSS feed to my Google account.

I look forward to new updates and will talk about this blog with
my Facebook group. Chat soon!

werbeagentur blog on 7/3/13

Terrific article! This is the type of info that are supposed to be shared around the
net. Disgrace on Google for not positioning this put up higher!
Come on over and consult with my web site . Thank you =)

Pflegeheim Blog on 8/4/13

It’s remarkable designed for me to have a web site, which is useful for my know-how.
thanks admin

start Showjumping on 9/17/13

Unquestionably believe that which you said. Your favorite reason seemed to be on the net the simplest thing to
be aware of. I say to you, I definitely get annoyed while people consider worries
that they just do not know about. You managed to hit the nail
upon the top and defined out the whole thing without having side-effects , people could take a signal.
Will probably be back to get more. Thanks

criminal defense on 9/18/13

We stumbled over here by a different website and thought I might check things out.

I like what I see so i am just following you. Look forward to finding
out about your web page repeatedly.

smallidea2077.jigsy.com on 9/30/13

The best position to start is using the centerpiece design.
In fact we employed to give a larger weighting than this to December because we knew the returns tend to
be greater. It just isn’t that all people are so rich to purchase stones.

Raina on 1/4/14

In fact, we loved it so much, we ordered a smaller second one, this
time with strawberry yoghurt and fresh straweberries. When I say a meal, I do not mean sitting down with a knife and
folk to eat having spent an hour cooking. Are living yoghurt assists to restore the germs in large intestine.

Bizcocho con yogur on 1/4/14

Hi mates, its enormous post concerning teachingand entirely explained, keep it up all the time.

Shipping Food To Canada Surrey on 12/15/14

Comments are not open at this time.