问题描述
这或多或少是过去的堆栈溢出问题,这是有关MVC应用程序的大多数入门资料倾向于在模型,视图和控制器之间呈现紧密联系的方式.例如,您将有一个由用户控制器修改的用户表,该用户控制器又将过滤后的数据推送到用户视图.我的印象是,许多MVC框架也倾向于反映这种模式.一切都很好,而且很好,但是除了用HTML表单构建和显示单调的事物列表之外,它并没有真正带给我任何帮助.
This is more or less a framework-centric version of a past Stack Overflow question, which is about how most introductory material on MVC applications tends to present a tight coupling between models, views, and controllers. For example, you'll have a User table that is modified by a User controller which in turn pushes filtered data to a User view. It's my impression that a lot of MVC frameworks tend to reflect this pattern as well. This is all fine and well for what it is, but it never really leads me to anything beyond building and displaying monotonous lists of things with an HTML form.
现在要查看的MVC框架是 Lithium ,作为一个聪明的PHP5案例研究,它似乎很有趣. 3种编码技术.一方面,Lithium具有Model
类,该类提供围绕单个表的包装对象,并抽象出一些简单的查询.另一方面,在将URL路由到控制器对象上的方法调用方面,有一个很好的约定,然后呈现为显示模板.
The MVC framework that looking at right now is Lithium, which seems quite interesting as a case study of clever PHP5.3 coding techniques. On one end, Lithium has a Model
class that offers wrapper objects around a individual tables, and abstracts away some simple queries. On the other end, it's got a nifty convention of routing URLs to method calls on controller objects, which then render to display templates.
但是在此过程中,我发现自己不知道将所有与表A中的数据与表B至Z中的数据相关的有趣逻辑放在哪里.或者至少,我不确定在哪里以与框架设计一致的方式放置此类逻辑.据我了解,Lithium的Model
抽象只不过消除了某些行级的插入/更新/删除样板而已,而控制器/视图的体系结构似乎主要与用户界面有关.我不想将很多业务逻辑放在从URL请求接收路由功能调用的同一Controller
类中.
But in the midst of this, I find myself at a loss as to where to place all of the interesting logic that relates data in table A to data in tables B through Z. Or at least, I'm not sure where to place such logic in a manner that's consistent with the design of the framework. To my understanding, Lithium's Model
abstraction doesn't do much more than eliminate some row-level insert/update/delete boilerplate, and the controller/view architecture seems mostly about user interface. I wouldn't want to put a lot of business logic in the same Controller
class that is receiving routed function calls from URL requests.
我的直觉是用一堆我自己的代码来填补空白,这些代码或多或少完全存在于框架之外.我不确定是否应该期望得到更多,但是鉴于锂电中其他所有部件的结构都十分僵化,这让人感到不满意,就像我可以自己编写样板代码减少的代码而无需花费过多的时间来了解锂离子源一样一个大框架.
My instinct would be to fill the gap with a bunch of my own code that exists more or less entirely outside of the framework. I'm not sure if I ought to expect more than that, but given how rigidly structured everything else is in Lithium, it feels somehow unsatisfying, like I could have just rolled my own boilerplate-reduction code without the overhead of grokking the source of a big framework.
我在这里想念什么?使用这种类型的框架是否有推荐的体系结构或哲学?
What am I missing here? Is there a recommended architecture or philosophy to using this type of framework?
推荐答案
Lithium必须记住的一件事是,尚无生产就绪版本(尽管某些站点正在生产中使用它).目前主要缺少的功能是模型关系.关系到位后,我认为您的问题将得到部分回答,因为这是创建更复杂的应用程序的重要组成部分.您可以在x-data分支中检查关系工作.
One thing you have to remember with Lithium is that theres no production ready release yet (although some sites are using it in production).The main missing feature right now is model relations. With relations in place i assume your question would be partially answered as that is an important brick in the big picture of creating more complex applications.You could check out the x-data branch where the work on relations should be ongoing.
对于编写域逻辑的第二部分,简单的答案是在模型中".例如,请参见扩展模型功能的这个(相当无用的)示例 .另一个要看的示例是开源微型应用程序Analogue,它是使用中的少数几个可用的开放式锂离子示例. 模拟模型类显示的模型稍微复杂一些.
For the second part of writing domain logic the simple answer is "in the model".See this (rather useless) example of extending model functionality for example.Another example to look at is the open source mini application Analogue that is one of the few working open examples of Lithium in use. The Analogue model class shows a slightly more meaty model.
最后是在M,V和C之间连接点的问题.锂控制器应主要将工作委托给模型,并在需要时重组输入数据.具有Post模型,PostsController和views/posts/add,index等的简单示例并不意味着您只需要在其中拥有Post :: all().PostsController :: view可能需要加载一组Comment模型.
And finally its a matter of connecting the dots between M, V and C.A Lithium controller should mainly delegate jobs over to models, and perhaps restructure the input data if needed. The simple examples of having a Post model, PostsController and views/posts/add,index,etc doesn't mean that you have to merely have Post::all() in there.PostsController::view need to load a set of Comment models probably.
因此,您当然会在其中扔很多自己的代码!否则,它不会是太多的应用程序.但是,将域逻辑与自然模型联系起来.
So you will toss a lot of your own code in there, of course! Else it would not be much of an application. But keep the domain logic tied to the model where it is natural.
- 需要验证博客文章中是否包含独特的头衔?编写验证器.
- 是否需要确保用户对帖子具有写权限?覆盖保存方法并进行验证或过滤.
- Need to verify that blog post has aunique title? Write a validator.
- Need to ensure the user has write access to the post? Override the save method and verify it, or filter it.
但是我认为我们需要等待关系和1.0版本的发布,才能充分判断如何才能最好地解决Lithium中的结构化应用.
But I think we need to wait for relations to land and a 1.0 release before we can fully judge how structuring applications in Lithium can be solved best.
这篇关于超越CRUD的锂电应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!