使用框架的概念性 MVC 问题

发布于 2024-10-08 08:16:53 字数 266 浏览 2 评论 0原文

当创建一个相当复杂的用于编辑数据库记录的表单时,我当前正在加载控制器,将请求发送到模型以制作表单,然后从模型本身加载输出,而不是将表单作为 $string 发送到要插入页面的视图。

我一直纠结于我们是否应该在模型或视图中创建表单(甚至需要访问数据库来创建表单,因为它是动态的),如果它是在模型中创建的,我们是否应该将其创建为字符串并将其发送到视图。

我知道这里没有“完美”的答案,但我们正在努力标准化,以便在引入开发人员时,我们以行业标准的方式做事(我们仍在学习)。使用代码点火器框架。

when creating a fairly complicated form that will be used to edit a database record I am currently loading the controller, sending the request to the model to make the form and then loading the output from the model itself instead of sending the form as a $string to the view to insert into the page.

I keep getting hung up on whether we should create the form (which needs to access the database even to create the form because it's dynamic) in the model or in the view and if it's created in the model, should we create it as a string and send it to a view.

I know there is no "perfect" answer here, but we are trying to standardize so that as we bring on developers, we've done things in an industry-standard way (which we are still learning). Using the Code Igniter framework.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼泪都笑了 2024-10-15 08:16:53

在我看来,这里有一个完美的答案:

模型处理事物的数据方面。显示表单以编辑模型中的数据是 UI 的职责,而 UI 则是视图的职责。

换句话说,模型不应该呈现它自己的形式。视图应该这样做。

In my mind, there is a perfect answer here:

The Model handles the data side of things. Displaying a form to edit the data in the Model is the responsibility of the UI which is the responsibility of the View.

In other words the Model shouldn't render it's own form. The View should be doing that.

往事随风而去 2024-10-15 08:16:53

不要通过确定视图不应包含任何服务器端代码来限制您的应用程序。如果要将数组传递给视图,则需要循环才能将此数据输出到浏览器。通过在模型中创建字符串并将其传递给视图,您必须在模型中创建大量 HTML,而这正是视图的用途。我还没有遇到过这样的情况:我无法避免在表单中至少使用一点 PHP,通常是将 POST 数组中的值添加到验证失败的输入表单中。

使用对你来说最干净、最明智的方法,不要自责你违反了规则——在这种情况下你没有违反规则。只要您的应用程序是安全的,并且它可以使用干净、可读和可维护的代码,那么就可以使用它来运行。

Don't limit your application by determining that the View should not contain any server side code. If you were to pass an array to the View then you would need to loop through in order to output this data to the browser. By creating the string in the Model and passing it to the View, you must be creating a lot of HTML in the Model which is what the View is for. I have yet to come across a situation where I have not been able to avoid using at least a little PHP in a form, usually to add the value from the POST array to an input form where it has failed validation.

Use the method that seems the cleanest and most sensible to you and don't beat yourself up you are bending the rules - in this case you aren't. As long as your application is secure and it works with clean, readable and maintainable code then run with it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文