FIXME it does seem possible to control other model through te use of $uses. requestAction() only returns rendered views, no data.

Model View Controller Architecture

Why am i writing this

This is a fundamental part of CakePHP. Understanding this gives you inside to how applications should be written. I’ve only just found out i’ve been doing things the wrong way.

So i’m probably not the right person who should be writing this. But eventually someone with greater understanding of the subject will come along add his knowledge to this page.

I’m also telling you te obvious stuff so the one with the greater understanding has something to start with.

The architecture

This specifically applies to CakePHP.

Other frameworks have a less strict interpretation of MVC, others low level implementation is different. Like Coaca only have one controller per application. In Ruby on Rails the model class returns objects instead of arrays.

Strict MVC means:

  • only ones Controller manipulates its Model
  • every controller action has its view (some don’t need one but people could end up viewing it)
  • every controller has its model (modelless controller are usable and usefull in CakePHP)

This is what i’ve come to understand of it (don’t take my word for it). The first constraint is the most important and is practically enforced in CakePHP.

Model

A model is mapped to a database. Im guessing this mapping is strict. It wouldn’t be of much use an empty model. A findAll() would just return an empty array.

If you retrieve a model or more instances of one an array is returned. This array contains the content of the database with the name of the fields of the model.

Associations are a very usefull part of a model. It makes it possible to retrieve other related models with one query on the main model. Model::$recursive is a usefull property wich limits or enhances the scope of the related models returned. http://wiki.cakephp.org/docs:understanding_associations

If default data should be passed or actions have consequences for other models this should be implemented on model level. (I’m not sure about this, some of it could be controller logic.) Default parameter shouldn’t be passed through hidden fields in a form (hackable, quite sure this is not the way). Create model methods like buildPost() or addComment().

View

Every controller action should have a view.

Some controller actions only return data. This will be discussed below. Yet a user could type in the url and end up at the controller-action-view-doesn’t-exist-error page.

Views acces the data set in the controller set with the controller::set() command. The also have acces to the models themselves. They could acces model methods wich translate the data in the model array. (Not sure this is supposed to be done, but the models aren’t object so.)

As i’ve said before default parameters shouldn’t be passed through a view.

Controller

Every model needs a controller.

And only ones contoller should manipulate its model.

This can be achieved by Object::requestAction() http://api.cakephp.org/class_object.html#6709fd2dd27063746f30f5c7168a82e9

You can request action on other controllers this way.

Controller::requestAction( $url, $extra = array() )

Look this up. It’s the key to seperating different controllers logic.

If your new use Bake bake.


hope this helps. — 2006/07/15 08:52

 
docs/mvc.txt · Last modified: 2006/07/17 07:15 by wakker