简体   繁体   English

超越CRUD的锂应用程序

[英]Lithium apps that go beyond CRUD

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. 这或多或少是过去Stack Overflow问题的以框架为中心的版本,该问题是关于MVC应用程序的大多数介绍性材料如何在模型,视图和控制器之间呈现紧密耦合。 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. 我的印象是很多MVC框架也倾向于反映这种模式。 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. 这一切都很好,很好,但除了用HTML表单构建和显示单调的事物列表之外,它从来没有真正引导我。

The MVC framework that looking at right now is Lithium , which seems quite interesting as a case study of clever PHP5.3 coding techniques. 现在正在研究的MVC框架是Lithium ,它作为巧妙的PHP5.3编码技术的案例研究似乎非常有趣。 On one end, Lithium has a Model class that offers wrapper objects around a individual tables, and abstracts away some simple queries. 一方面,Lithium有一个Model类,它提供了围绕单个表的包装器对象,并抽象出一些简单的查询。 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. 另一方面,它有一个很好的约定,即路由URL到控制器对象上的方法调用,然后呈现给显示模板。

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. 但是在这期间,我发现自己不知道将表A中的数据与表B到Z中的数据相关联的所有有趣逻辑放在哪里。或者至少,我不知道在哪里放置这样的逻辑的方式与框架的设计一致。 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. 根据我的理解,Lithium的Model抽象除了消除一些行级插入/更新/删除样板外没有什么作用,控制器/视图架构似乎主要是关于用户界面。 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. 我不想在接收来自URL请求的路由函数调用的同一个Controller类中放置大量业务逻辑。

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. 我不确定我是否应该期待更多,但考虑到Lithium中其他所有内容的结构是如何严格的,它感觉某种程度上不令人满意,就像我刚刚推出了自己的样板减少代码而没有花费很多资源来解决这个问题。一个大框架。

What am I missing here? 我在这里错过了什么? Is there a recommended architecture or philosophy to using this type of framework? 是否有推荐的架构或哲学来使用这种类型的框架?

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. 您可以查看x-data分支,其中关系的工作应该在哪里进行。

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. 另一个值得关注的例子是开源迷你应用程序Analogue,它是使用中少数工作开放的锂的例子之一。 The Analogue model class shows a slightly more meaty model. Analogue模型类显示了一个稍微多肉的模型。

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. 最后是连接M,V和C之间的点。锂控制器应该主要将作业委托给模型,并且如果需要可能重新构造输入数据。 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. 使用Post模型,PostsController和views / posts / add,index等的简单示例并不意味着您必须只有Post :: all()。 PostsController::view need to load a set of Comment models probably. PostsController :: view需要加载一组Comment模型。

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 a unique 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. 覆盖save方法并对其进行验证,或对其进行过滤。

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. 但我认为我们需要等待关系到达并发布1.0版才能完全判断如何最好地解决Lithium中的结构化应用问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM