使用 HMVC (Kohana) 的知识损失
我对 Kohana 的 HMVC 模型有疑问。我真的很喜欢在相同原则 (MVC) 内将内容分离为组件的想法,以便扩展和更轻松地创建 api。
尽管我遇到了一些缺点,比如知识的丧失。
例如,我可以有名为新闻和类别的组件。最重要的是站点控制器,它们仅委托完整的请求并通过这些组件收集站点模板的所有 html/数据。
Site controller
/ \
/ \
categories news
当我想要侧边栏的所有类别时,我可以调用:/categories/list。当我想要新闻时,我可以拨打 /news/ID。两者都返回 html。
如果我有一个新闻阅读站点控制器,其主要任务是显示新闻文章,则该控制器将获取侧边栏的所有类别。接下来它将获取新闻项。
Newsreadon
/ \
/ \
categories/list (html) news/<id> (html)
当我显示站点模板中的所有元素时,我想知道 html 标题标签的新闻“标题”,但我不知道,因为我返回了 html。
我觉得有多种解决方案:
- 将标题存储在某个注册表中(但如果我将组件扩展到其他服务器并使用 HTTP,则会丢失)。所以考虑到扩展性,没有什么好的选择。
- 返回带有“title”和“html”字段的 json。
- 读出 html 的某些部分,例如标题的 h2
解决方案 2 似乎是最不令人讨厌的,并且保持无状态通信完好无损。
我真的很想知道你将如何以优雅的方式解决这个问题?我错过了“那个”解决方案吗?
编辑: 通过阅读 kohana 的有趣内容来了解 HMVC: http://techportal.inviqa.com/2010/ 02/22/缩放-web-applications-with-hmvc/
I have a questions regarding the HMVC model by Kohana. I really like the idea of separating stuff as components within the same principles (MVC) for scaling and easier api creation.
Though there are some drawbacks I’m experiencing like loss of knowledge.
For example, I could have components called news and categories. On top of that are the site controllers that just delegate the full request and gathers all html/data for the site templates through these components.
Site controller
/ \
/ \
categories news
When I want all categories for a sidebar, I could call: /categories/list. When I want a news item I could call /news/ID. Both return html.
If I have a newsreadon site controller which main task is to show a news article, this controller will get all categories for the sidebar. Next it will fetch the news item.
Newsreadon
/ \
/ \
categories/list (html) news/<id> (html)
When I display all elements within the site template I want to know the 'title' of the news for the html title tag, but I can't know because I return html.
I feel like there are multiple solutions:
- Store title in some registry (but this is lost if I scale the component to a other server and use HTTP). So in view of scaling, no good choice.
- Return json with 'title' and 'html' fields.
- Read out some part of the html like h2 for the title
Solution 2 seems to be the least nasty and keeps the stateless communication in tact.
I'm really wondering how you would solve this in a elegant way? Am I missing 'the' solution?
EDIT:
Interesting read to understand HMVC by kohana:
http://techportal.inviqa.com/2010/02/22/scaling-web-applications-with-hmvc/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果此控制器的目的是显示新闻文章,那么我不明白是否需要额外的控制器。考虑更改您的应用程序设计,也许 Newsreadon 扩展控制器以显示新闻文章。
If this controllers' purpose is to display a news article then I don't understand the need for an additional controller. Consider changing your application design, perhaps Newsreadon extends the controller to display the news article.
由于您使用
news/
显示新闻,因此父控制器可能知道新闻的 ID。由此,只需从模型中获取新闻标题即可。这才是正确的做法。尝试从子请求中提取一些数据将使您的应用程序耦合更紧密,并且从长远来看更难以维护。Since you display the news using
news/<id>
, presumably the parent controller knows the ID of the news. From that, just get the news title from your model. That would be the proper way to do it. Trying to extract some data from sub-requests will make your application more tightly coupled and more difficult to maintain on the long run.