处理 MVC 中的片段和视图
如果我的网站上有一个页面,必须显示 4 或 5 个片段(新闻提要、事件提要等),所有这些都与不同的数据(在不同的模型和数据库表中)相关,那么处理生成的明智方法是什么片段内容和布局?我可以使用一个包含静态函数的单个片段控制器,每个静态函数返回一个填充有相关数据的视图。但是这个代码片段控制器中的每个函数都将与不同的数据/模型交互,所以我不确定这是什么 OOP。或者,我可以向每个处理每个关联数据集的控制器添加一个静态函数 - 例如在 News_Controller 中,以及显示所有新闻项、单个新闻项等的函数,我可以添加一个静态函数只需返回我需要的视图即可生成新闻提要。我认为这种方法可能有效,因为我不想为这些简单的片段实例化对象,因此在相关控制器中使用静态函数是有意义的。这里有点意识流,但我有道理吗?!
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?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
许多框架都有一些“部分”的概念,通常用于此类事情。
由于这些部分通常是只读的,并且通常显示在每个页面(或一些定义明确的页面集)上,因此您可以访问它们,而无需像页面一样考虑控制器。
换句话说 - 请记住,如果您的视图/布局代码直接与您的模型对话,只要它只是询问它们就可以了。
我一直在做这样的事情:
layout.php:
Model_News::latest() 可能会实现一些缓存等,因为这是布局代码,我可能不想在每个请求上都访问数据库。
无需控制器膨胀!
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:
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!
您可能想研究一下分层模型-视图-控制器(HMVC)。它特别适合制作带有片段的页面。我相信 Kohana 实现了它。
基本上,您有多个模型-视图-控制器三元组,每个三元组负责每个片段。因此,您可以有一个控制器来访问新闻数据库并为不同类型的新闻显示加载各种小视图。
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.
您可以构建 MVC 模式来提供一种机制,允许已设置的数据作为数组返回。例如,通常对
/news/feed/
的 Web 请求会导致相关数据被设置并随后传递到视图。除了网络请求之外,系统还应该允许控制器发出这样的请求,即$vars = $this->call('/news/feed/');
系统将识别内部请求并返回设置的变量而不是调用视图。这使您可以保持控制器和模型的凝聚力。
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.