tags:

views:

786

answers:

2

I'm new to the MVC pattern but have been trying to grasp it, for example by reading the documentation for the CakePHP framework that I want to try out. However, now I have stumbled upon a scenario that I'm not really sure how to handle.

The web site I'm working on consists of nine fixed pages, that is, there will never exist any other page than those. Each page contains something specific, like the Guest book page holds guest book notes. However, in addition, every page holds a small news box and a short fact box that an admin should be able to edit. From my point of view, those should be considered as models, e.g. NewsPost and ShortFact with belonging controls NewsPostController and ShortFactController. Notice that they are completely unrelated to each other.

Now, my question is, how do I create a single view (web page) containing the guest book notes as well as the news post box and the short fact? Do I:

  1. Set up a unique controller GuestBookController (with an index() action) for the guest book, so that visiting www.domain.com/guest_book lets the index action fetch the latest news post and a random short fact?

  2. Put static pages in /pages/ and in let the PagesController do the fetching?

  3. < Please fill in the proper way here. >

Thanks in advance!

A: 

I am not completely familiar with the cakephp framework, but just using php you could just do a

require('../commonViews/NewsFeedView.php');

for each page that needs the news box and this will just import everything from that php into the page.

So in NewsFeedView you could put the code for grabbing the lastest news from the db and echoing it out into a div.

Again, there might be a better way to do this with cakephp templates, but I've never used cakephp so I can't say.

asperous.us
+2  A: 

It sounds like you need to look into elements, or else you may be able to embed this into the layout - but its neater to use an element if you ask me, keep the things separate.

http://book.cakephp.org/view/97/Elements

These allow you to have create small views that you are able to embed into other views.

You may also need to put some logic into the AppController (remember all other controllers extend the app controller) to load the data required for these views. The beforeRender function should be useful for this - its one of the hook functions cakephp provides, so if you define it on a controller, its always called after the action is finished before the view is rendered.

Something like this in your AppController should help:

function beforeRender() {
  $this->dostuff();
}

function doStuff() {
  // do what you need to do here - eg: load some data.
  $shortfacts = $this->ShortFact->findAll();
  $news = $this->NewsPost->findAll();
  // news and shortfacts will be available within the $shortfacts and $news variables in the view.
  $this->set('shortfacts', $shortfacts);
  $this->set('news', $news);
}

If there are models you need in the app controller for use within this doStuff method, then you need to define them within uses at the top of the AppController

class AppController {
  var $uses = array('NewsPost', 'ShortFact');
}
benlumley
Great! Thanks a lot for your terrific answer! That example code just hit the nail on the head. I will try this out as soon as possible. Now it's time for bed, though.
joelpet
hope it gets you where you need to be. bed time here as well, guess you are also in europe!
benlumley
Yes, I think it will! However, let's say (hypothetically) there were just a subset of the views (web pages) that were to include a news box and a short fact. Wouldn't that imply that the NewsPost and ShortFact models would be loaded unnecessarily sometimes? How would you solve that? A super Controller class that only the views in question would extend?(Yep, Sweden.)
joelpet
Yes, you will likely include unnecessary components sometimes unless you're strict about use of bindModel() and unbindModel(). Those allow you to dynamically bind/unbind models to controllers.However, I wouldn't worry too much about the speed right now. Realistically, if you need speed, CakePHP is not the way to go ...
Travis Leleu
What travis said!But for the record, I think your approach of putting the beforeRender in a controller that extends app controller and that the relevant controllers extend should work.
benlumley
Thanks for great answers! Just out of curiosity, what approach to web programming is suitable if speed is important?
joelpet