帮助理解 MVC 中控制器与视图的内容

发布于 2024-10-09 02:25:15 字数 433 浏览 3 评论 0原文

我是 MVC 新手,我正在通过 Codeigniter 框架介绍自己。作为练习应用程序,我正在创建一个简单的博客应用程序,该应用程序能够创建、查看、编辑和删除帖子。

对于“编辑帖子”页面,我使用与“创建新帖子”相同的视图文件,并简单地使用用户编辑旧帖子时从模型中获取的数据填充表单字段。

这看起来一切都很好,但我对控制器和视图中存储的逻辑有点困惑。我显然需要一些逻辑来告诉表单预先填充表单字段(如果我正在编辑帖子)或将所有内容留空(如果它是新帖子)。还有其他事情,例如视图页面需要知道在页面顶部显示什么标题:“创建新帖子”或“编辑帖子”。

这个逻辑去哪儿了?它会出现在视图中吗?还是放在控制器中?为什么?我开始将所有逻辑放入视图文件中,因为我读到应该保持控制器代码最少,但是现在我的视图文件顶部有一个很大的 php 代码块,用于处理变量,我不确定如果这是正确的方法。

任何建议将不胜感激!

I'm new to MVC and I'm introducing myself through the Codeigniter framework. As a practice application I'm creating a simple blog application that gives the ability to create, view, edit, and delete posts.

For the "edit post" page, I'm using the same view file as my "create new post" and simply populating the form fields with the data I'm getting from my model when the user is editing an old post.

This seems all fine and dandy but I'm a bit confused on what logic is stored in the Controller vs. View. I obviously need some logic that tells the form to pre-populate the form fields (if I'm editing a post) or to leave everything blank (if it's a new post). There's also other things such as the view page needs to know what title to display at the top of the page: "Create New Post" or "Edit Post".

Where does this logic go? Does it go in the View? Or does it go in the Controller? and Why? I started putting all that logic inside the view file because I've read that I should keep the controller code minimal, however I now have a big php code block at the top of my view file that's dealing with variables and I'm not sure if this is the correct approach.

Any advice would be much appreciated!

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

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

发布评论

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

评论(1

七度光 2024-10-16 02:25:15

我显然需要一些逻辑来告诉表单预先填充表单字段(如果我正在编辑帖子)或将所有内容留空(如果是新帖子)。

查看表单助手中的 set_value() 函数。您可以在视图中执行以下操作:

<input type="text" name="subject" value="<?php echo set_value('subject', $subject); ?>" size="50" />

如果是新帖子,请在从控制器加载视图时将空字符串作为 $subject 传递。

如果正在编辑,请将正在编辑的帖子主题传递为 $subject。

另外,如果用户提交表单并且出现错误并且您需要重新加载表单,set_value() 将返回用户刚刚发布的内容(即 $_POST['subject'])。

还有其他事情,例如视图页面需要知道在页面顶部显示什么标题:“创建新帖子”或“编辑帖子”。

只需将名为 $page_title 的变量从控制器传递到视图,并相应地设置值即可。这很常见,尤其是当您开始构建可重用模板时。

这个逻辑去哪儿了?它会出现在视图中吗?还是放在控制器中?为什么?

视图中应该几乎没有逻辑。如果必须的话,可以使用简单的 if/else 语句,并循环遍历数据数组。

即使控制器也不应该有太多逻辑。它主要负责充当模型、视图和库之间的中间人。

我读到应该保持控制器代码最少

这与控制器与模型有关,而不是与视图有关。保持控制器较小,并将尽可能多的逻辑放入模型和库中是一种很好的做法。也称为“瘦控制器,胖模型”。

I obviously need some logic that tells the form to pre-populate the form fields (if I'm editing a post) or to leave everything blank (if it's a new post).

Check out the set_value() function in the Form Helper. You can do something like this in your view:

<input type="text" name="subject" value="<?php echo set_value('subject', $subject); ?>" size="50" />

If it's a new post, pass empty string as $subject when loading the View from the Controller.

If it's editing, pass the subject of the post that is being edited as $subject.

Also, if a user submits the form and there are errors and you need to reload the form, set_value() will return what the user just posted (i.e. $_POST['subject']).

There's also other things such as the view page needs to know what title to display at the top of the page: "Create New Post" or "Edit Post".

Just pass a variable named $page_title from your Controller to your View, and set the value accordingly. This is pretty common, especially when you start building reusable templates.

Where does this logic go? Does it go in the View? Or does it go in the Controller? and Why?

There should be almost no logic in the View. Maybe simple if/else statements if you must, and loops to iterate through arrays of data.

Even Controller should not have much logic in it. It is mainly responsible for acting as a middle-man between the Models, the Views and the libraries.

I've read that I should keep the controller code minimal

That has to do with Controllers vs. Models, not the Views. It is good practice to keep Controllers smaller, and put as much of the logic as possible into the Models and the libraries. Also referred to as "Skinny Controllers, Fat Models".

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