项目MVC结构的问题
在我的项目中,我有大约 20 个 html 表单,其中有许多不同的 。这个
在每种形式中都是唯一的,并且它们之间不会重复。对于每个表单都有一个脚本,它从表单获取数据,生成特定文件,并将其推送到浏览器。就是这样。没有数据库、管理、登录名/密码和其他常见的网络应用程序内容。
例如,在 php 中,项目结构可以是这样的:
forms/
------->form1/
--------------->index.html
--- ------------>脚本/
----------------------->index.php
------ ->form2/
--------------->index.html
-------------->script/
------ ----------------->index.php
等。它非常清晰,并且可以生成漂亮的 url,例如:
www.website.com/forms/form1
,但在 Ruby-on-Rails 中存在 MVC 模式。我不知道如何组织这样的项目的结构。怎样做才对呢?毕竟我不应该制作 20 个不同的控制器,对吗?
in my project i have about 20 html forms with many different <input>
's. This <input>
's are unique in every form and they don't repeated between them. For each form there is a script which get data from form, generate specific file, and push it to the browser. and that's it. no databases, admin, logins/passwords and other usual web-app stuff.
so for example in php, project structure can be something like this:
forms/
------->form1/
--------------->index.html
--------------->script/
----------------------->index.php
------->form2/
--------------->index.html
--------------->script/
----------------------->index.php
and so on. It's quite clear and it makes pretty urls like:
www.website.com/forms/form1
but in Ruby-on-Rails there is a MVC pattern. And i have no idea how to organize structure with project like that. How to make it right? I should not to make 20 different controllers after all, right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您确实制作了 20 个控制器。与 PHP 结构中有 20 个 script/index.php 文件的方式大致相同。 Rails 结构看起来像这样。
其中控制器为您想要执行的每个操作(例如新建、编辑等)提供了一个方法
。layouts/application.html.erb 文件将包含所有页面中一致的所有标记。
最后,不要试图与结构作斗争。由于约定,它可以启用最少的代码,如果您尝试创建自己的结构,您将创建一个痛苦的世界。
Yes you do make 20 controllers. In much the same way as you have 20 script/index.php files in your PHP structure. The rails structure would look something like this.
Where the controllers have a method for each action that you want to perform e.g. new, edit etc
The
layouts/application.html.erb
file will contain all of the markup that is consistent across all of your pages.Finally, don't try and fight the structure. It is there to enable minimal code because of convention, if you try and create your own structures you will create a world of pain.
由于表单没有数据库后端,我将创建一个 FormsController,其中包含 20 个方法(form1、form2、form3,...)。
然后,您的网址看起来几乎相同
\forms\form1
、\forms\form2
...即使您使用 20 个控制器,Ruby 中也有很多方法可以删除重复的代码。因此,即使在普通的 MVC 中,我也有过包含 140 个模型的应用程序,并且同样有许多控制器,其中大多数控制器只有一行,并且视图完全是通用的。
仅仅因为某个解决方案在 PHP 中看起来很简单,并不意味着不存在更好的解决方案。比如MVC。它可能看起来会导致更多的代码、更多的文件、更多的工作,但结构的存在是有充分理由的,它干净地分离了关注点,并且每个文件本身变得更加清晰和更容易理解。
Since there is no database backend for the forms, i would create one FormsController, with 20 methods (form1, form2, form3, ...).
Your urls would then look almost the same
\forms\form1
,\forms\form2
...Even if you would use 20 controllers, there are many ways in ruby to remove duplicate code. So even in a normal MVC, i have had applications with 140 models, and likewise as many controllers, most of these controllers had only one line and the views were completely generic.
Just because a certain solution seemed simple in PHP, it doesn't mean better solutions don't exist. Like for instance MVC. It might seem to induce more code, more files, more work, but the structure is there for good reason, it seperates the concerns cleanly, and each file in itself becomes more clear and easier to understand.