Using Uses in AppController

Don't do it.

Or maybe there's a better way to do it. The problem I ran into was that I had some Model code that needed to run on every page. So, I thought that I'd add the model to the uses variable in the AppController.

The problem with that, as it turns out, is that none of the controllers overrode this property. This meant they couldn't access their models properly. This made leaving a comment here within the last 12 hours impossible. Whoops!

If ever there was a reason for me to get into test-driven development, this was probably it.

Published October 26, 2006
Categorized as CakePHP
Short URL: https://snook.ca/s/707

Conversation

10 Comments · RSS feed
Luke said on October 26, 2006

Jon,
Cake PHP seems to be your framework of choice. Did you check out RoR at some point and find things that you didn't like or couldn't do with it? I am much more familiar with PHP than Ruby and looking for a framework to adopt, but keep finding rave reviews about RoR (obviously from RoR users). I guess I'm just looking for the opinion of someone on the other side of the fence. :)

Jonathan Snook said on October 26, 2006

I checked out RoR only to the extent of trying to understand the language and implementation a little more. I ended up going with something PHP-based because of its familiarity and due to the popularity of PHP. I felt it'd be easier to find developers and have access to a larger pool of modules, etc.

RoR is a great framework and I definitely recommend that people try it out, if they can.

Andy Kant said on October 26, 2006

I've tried most major web technologies and I have to say that Ruby is probably my favorite overall language now. However, it isn't nearly as mature as PHP in regards to available libraries and especially documentation. I don't see RoR gaining a ton of ground until the Ruby and Rails documentations are up to par with php.net and MSDN.

Then again, the motto of most Rails teams seems to be AGILE AGILE AGILE...and with agile, documentation is often at the bottom of the list of things to do despite how necessary it is.

Raymond Olavides said on November 09, 2006

Hi Jonathan,

I'm currently using cakephp in an application that we're developing, I use the variable 'uses' a lot and I don't seem to be encountering what you've mentioned here. Pardon me if I don't get what you mean by "none of the controllers overrode this property. This meant they couldn't access their models properly" - do you mean that the model that uses that 'uses' variable to load other models inside itself can no longer access it's own methods??? If this is what you meant, all we need to do is add that model's name to the uses variable as well.

Maybe, CakePHP developers should consider having the variable 'uses' add the calling model by default.

Jonathan Snook said on November 09, 2006

Raymond, I refer specifically to 'uses' in app_controller.php.

Mariano Iglesias said on December 09, 2006

Hi Jonathan,



Eventhough I don't like to use $uses in AppController (I tend to use components when needing that) it is true that this is no longer an issue in newest versions of CakePHP. Now the $uses property on AppController behaves the same way as $components, or $helpers.

Igor said on December 16, 2006

thanks for the opportunity to discuss cake!

I prefer to load models only when they are needed too, but is there some method allowing this except loadModel('Mymodel') ? as seems (no enough documentation :)) this method does not accept many models at once - what is not good for me

Matthew said on March 21, 2007

If you need access to a specific model inside your app_controller, this is the code I use:

loadModel('SomeModel');

$this->SomeModel =& new SomeModel();

jan.ptacek said on July 16, 2007

i still experience the same problem with $components var in a plugin app_controller,
i have to do it this way in the plugin app_controller:


function __construct() {
// plugin appcontrollers components do not behave well
$this->components = am($this->components, array('Cookie'));
parent::__construct();
}
Marco Pegoraro said on September 10, 2007

I use the "uses" property in AppController to load some common used models like the "User" or "Group" model wich are used in every page in my applications.

Controllers who need dedicated models can load them with naming rules or with the "loadModel" directive or by overriding the "Uses" property or by extending it in the constructor.

I find very usefull to be allowed to load some "commonly used" models in my AppController class.

Sorry, comments are closed for this post. If you have any further questions or comments, feel free to send them to me directly.

Want to learn about scaling CSS for large projects?

I'm available for full and half-day workshops on scalable CSS architecture. I can provide on-site training for your team. Interested?
Get in touch.