Rails 3 中的分层 MVC?
我读过有关 HMVC(分层模型视图控制器)及其灵活的结构。
看看这张图片:
http://techportal .inviqa.com/wp-content/uploads/2010/02/MVC-HMVC.png
我想知道 Rails 3 插件是否是 Rails 3 中 HMVC 的答案?
I've read about HMVC (Hierarchic Model View Controller) and it's flexible structure.
Have a look at this picture:
http://techportal.inviqa.com/wp-content/uploads/2010/02/MVC-HMVC.png
I wonder if the Rails 3 plugins are the answer to HMVC in Rails 3?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据对 Toby 答案的评论,您似乎希望能够将 MVC 应用程序用作新应用程序中的组件。 Rails Engines(参见 http://rails-engines.org)提供了此功能。您只需安装引擎 gem 并将应用程序放置在供应商/插件中,并且其模型/视图/控制器都可以访问。
这并不真正符合 HMVC,其中新应用程序中的控制器委托给其他控制器。但和托比一样,我看不到这样做的好处。
引擎方法的优点在于,您只需将模型的版本添加到新应用程序 app/model 文件夹中即可覆盖插件中的任何模型(同样适用于视图和控制器)
我已经覆盖了 app/views/布局使我的身份验证应用程序/插件与它所包含的应用程序具有相同的外观和感觉。
对于 Rails 3 Railtie 取代了引擎并受到官方支持(并且实际使用 - Action Mailer 是一个 Railtie 插件。我没有使用过不过,我自己也
可以在 http://edgeapi.rubyonrails.org/classes 上查看 。 /Rails/Railtie.html
这里也有一篇很好的文章http://www.igvita.com/2010/08/04/rails-3-internals-railtie-creating-plugins/
Based on the comments to Toby's answer it seems that you would like to be able to have MVC apps used as a component within a new app. Rails Engines (See http://rails-engines.org) provides this functionality. You simply install the engines gem and place apps in vendor/plugins and its modles/views/controller are all accessible.
This does not really conform to HMVC where the controllers in the new app delegate to other controllers. But like Toby I do not see the advantage of that.
What is nice about the Engines approach is that you can over ride any of models in the plugin by just adding a version of the model to the new apps app/model folder (same applies for views and controllers)
I have overidden app/views/layouts to give my Authentication app/plugin the same look and feel as the application it is included in.
For Rails 3 Railtie takes the place of engines and is officially supported (and actually used - Action Mailer is a Railtie plugin. I have not used it myself yet though.
Check it out at http://edgeapi.rubyonrails.org/classes/Rails/Railtie.html
A nice write up on it is also here http://www.igvita.com/2010/08/04/rails-3-internals-railtie-creating-plugins/
Rails 已经有插件很长时间了。
我怀疑是否存在技术原因导致控制器无法分派到另一个控制器,从而沿链传递请求对象。我只是不知道这样做会得到什么 - 该图看起来像意大利面条。
对我来说这是对 MVC 的滥用。我建议将逻辑推入较低级别的模型和类并创建一个面向该逻辑的单个控制器,而不是创建一个控制器链,这要简单得多且更易于维护。
Rails has had plugins for a long time.
I doubt there is a technical reason why a controller couldn't dispatch to another controller, passing the request object along a chain. I just don't know what you gain by doing so - the diagram looks like spaghetti.
To me it's a misuse of MVC. I would suggest it is much simpler and more maintainable to push logic into lower-level models and classes and create a single controller that fronts the this logic, rather than creating a chain of controllers.
在 Rails 3 博客文章中,DHH 提到了 Cells 项目。我还没用过,但我要去看看。
购物车示例很好地展示了这种功能如何清理您的应用程序代码。检索数据的代码应放置在控制器中的某个位置。在每个操作中或在过滤器之前。 Cell 似乎是更好的解决方案。
In the Rails 3 blog post, DHH mentioned the Cells project. I haven't used it but I am going to check it out.
The cart example shows well how that kind of functionality might clean up your application code. Code which retrieves data should be placed somewhere in controller. In every action or in a before filter. The Cell seems to be much better solution.
请查看这篇 rubyonrails-talk 帖子:https://groups.google。 com/forum/#!topic/rubyonrails-talk/0c4TT7UOGCw
Please look at this rubyonrails-talk post: https://groups.google.com/forum/#!topic/rubyonrails-talk/0c4TT7UOGCw