1

I'm trying to build a simple PHP MVC framework. What I have right now is a basic set-up with a router that sends the request to the right controller, so I'm making the URL

/blog/[id]

Resolve to a class method like

Blog->singlePost();

Within singlePost(); I then interact with the model to get the right blog post and send it off to the view. The basic MVC triad is all there.

But pages are more complex than this. I've got a sidebar that I need to fill with data (from the database and other sources). I've got a main menu to build, I need to show the details for the current logged in user, and more. But, that said, I don't need all of these sections on every page.

My question is, where do I initiate/build all these various shared sections for the view?

My options that I can think of are:

  1. Have every controller send a request for the required additional sections to be built (would have to update every controller each time something is added, no way)
  2. When mapping the URL's, define a group that the route belongs to that loads in all the required sections (Not ideal, not every section will be needed on every page, could get messy)
  3. Have the View (or Template) embed these sections on-demand. So the data for these sections would not be retrieved until the view explicitly asks for the data (The basic functionality of this seems quite appealing, but does it break the rules of what the view should do?)
  4. Something else?

3 Seems quite appealing to me because I'd like for my templates (that will be dynamic and switchable via a template system) to be able to request a section on any page. So one template may show the "Top 10 Blog Posts" on the homepage, but another may not, so there's no point in loading it. I see that with Symfony/Twig, you can do this: http://symfony.com/doc/2.0/book/templating.html#embedding-controllers

Is this a good idea for various modules on a site? Or should everything be loaded before the view starts being processed?

Andy-V
  • 11
  • 1
  • Just as a curiosity, are you doing this as an academic exercise or are you intending to actually have production code based on this? – adear11 Aug 28 '13 at 00:06
  • My proposal would be: use fully implemented view instances with presentation objects. [This post](http://stackoverflow.com/a/16596704/727208) should explain the details. – tereško Aug 28 '13 at 00:06
  • @adear11 It's both, hopefully. I know that using a pre-existing framework would speed things up and I also don't expect to build something better than the experts. I'm not in a rush and interested in learning. – Andy-V Aug 28 '13 at 00:12
  • @tereško Thanks, will have a look – Andy-V Aug 28 '13 at 00:12

0 Answers0