Fragment and view handling in MVC

if I have a page on my site where I have to show 4 or 5 fragments (news feeds, an event feed, etc.), all related to different data (in different models and db tables), then what is a reasonable way to process generating content and fragment layout? I could use one snippet controller that contains static functions, each of which returns a view filled with the corresponding data. But every function in this snippet controller will interact with different data / models, so I'm not sure how to do it. Or I can simply add a static function for each of the controllers that deal with each associated dataset - for example, in News_Controller, as well as functions to display all the news, individual news, etc. I could add a static function to generate news channel, just returning the view to me. I think this approach may work, because I do not want to instantiate an object for these simple fragments, so the presence of static functions in the respective controllers makes little sense. A bit of a stream of consciousness is here, but do I feel any sense ?!

+4
source share
3 answers

Many frameworks have the concept of β€œpartial,” which are commonly used for this kind of thing.

Since these partial files are usually read-only and often displayed on every page (or in a specific set of pages), you can access them without thinking from the point of view of controllers like you for the page.

In other words - remember that this is great if your view / layout code speaks directly to your models, if it only interrogates them.

I've been doing this all this 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 () may implement some caching, etc., since this is the layout code, and I probably don't want to hit db on every request.

No need to inflate the controller

+2
source

You might want to explore the hierarchical model-view-controller (HMVC). This is especially suitable for creating snippet pages. I believe that Kohana implements it.

Basically, you have several Model-View-Controller triads, each of which is responsible for each fragment. Thus, you can have a controller to access the news database and download a variety of small presentations to display different types of news.

+1
source

You can archive your MVC pattern to provide a mechanism for providing the data that was set to return as an array. For example, usually a web request to /news/feed/ will cause the relevant data to be installed and subsequently transferred to the view. In addition to web requests, the system should also allow the controller to execute such a request, i.e. $vars = $this->call('/news/feed/'); . The system will recognize the internal query and return the given values, and not refer to the view.

This keeps your controllers and models consistent.

0
source

Source: https://habr.com/ru/post/1310264/


All Articles