tags:

views:

42

answers:

3

Hi, if I have a page on my site where i have to show 4 or 5 snippets (news feeds, event feed etc), all relating to different data (in different models and db tables), then what is a sensible way to handle the generation of the snippet content and layout? i could use a single snippet controller which contains static functions which each return a view populated with the relevant data. But each function in this snippet controller will be interacting with different data/models, so i'm not sure how OOP this is. Or, I could just add a static function to each of the controllers which are dealing with each associated set of data - e.g in a News_Controller, as well as functions to show all news items, individual news items etc, i could add a static function to generate the news feed by simply returning the view I need. I think this approach might work, as i don't want to instantiate an object for these simple snippets, so having static functions in the relevant controllers makes a bit of sense. Bit of a stream of consciousness here, but am i making any sense?!

A: 

You can architect your MVC pattern to provide a mechanism for allowing data that has been set to be returned as an array. For instance, normally a web request to /news/feed/ would result in pertinent data being set and subsequently passed to a view. In addition to web requests, the system should also allow a controller to make such a request, i.e., $vars = $this->call('/news/feed/'); The system will recognize the internal request and return the set vars rather than invoking a view.

This allows you to keep your controllers and models cohesive.

webbiedave
+1  A: 

You may want to look into Hierarchical Model-View-Controller (HMVC). It's especially suited to making pages with snippets. I believe Kohana implements it.

Basically, you have multiple Model-View-Controller triads each responsible for each snippet. So you could have a controller to access the News database and load a variety of small views for different types of news displays.

Lotus Notes
+2  A: 

Many frameworks have some notion of "partials", which are typically used for this kind of thing.

Since these partials are generally read-only, and are often displayed on every page (or some well-defined set of pages), you can approach them without thinking in terms of controllers like you would for a page.

In other words -- remember that it's fine if your view/layout code talks directly to your models, as long as it's only interrogating them.

I do things like this all the time:

layout.php:

<div id="newsWidget">
<?PHP $news = Model_News::latest(); ?>
<?PHP foreach($news as $newsitem): ?>
<!-- output some news headlines -->
<?PHP endforeach; ?>
</div>

Model_News::latest() might implement some caching, etc, since this is layout code and I probably don't want to hit the db on every request.

No controller-bloat necessary!

timdev
frameworks differ in the efficiency of partials. I've used a similar approach to the one recommended by timdev along with caching because my partials appeared on every page of a busy site. I find it tricky to balance the "rightness" of a solution with the practicalities of getting the job done quickly and delivering a result. I'll often start with something simple and ugly and when the "code smell" gets too bad or it i need to use it somewhere else, will refactor it into something nicer and "righter" (grin)
Steve
Thanks Tim, this is the type of thing that I can get bogged down in, even though it involves simple features and only a couple of lines of code for each snippet. Your approach involves the least amount of code and repetition, and doesn't involve me creating new controllers or helpers for such a simple task. Makes perfect sense! Anytime there is slightly more complex code required than the News example, then I think I will add a static function to the most relevant controller.
kenny99