CodeIgniter 控制器和 AJAX
编辑:上一个标题,因为没有人读这个问题:“如果我在 MVC 框架中进行 AJAX 调用,模型上的“getter”控制器方法是否常见?”
这一切都在标题中:如果我想进行 AJAX 调用来删除用户,那么 DB 代码显然位于模型 $this->userModel->delete($id)
中。
如果我通过 AJAX 进行所有 CRUD 调用,我是否只有直通控制器方法来将这些模型调用公开到 URL?
function delete($id) {
$this->userModel->delete($id);
}
ETC?这看起来很愚蠢,但也有道理……但这也让我觉得我错过了一些东西。这是最常见的模式吗?
谢谢!
EDIT: Previous title, because no one was reading the question: "If I'm making AJAX calls in an MVC framework, is it common to have 'getter' controller methods over the model?"
It's all in the title: If I want to make an AJAX call to delete a user, that DB code clearly lives in the model $this->userModel->delete($id)
.
If I'm making all of the CRUD calls via AJAX, do I just have passthrough controller methods to expose those model calls to a URL?
function delete($id) {
$this->userModel->delete($id);
}
etc? This seems silly, but it also makes sense... but it also makes me feel like I'm missing something. Is this the most common pattern?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当谈到 MVC 框架下的 Ajax 时,我通常倾向于让每个函数都带有指定的关键字,例如 fetch/set。
像这样
回答您的问题:是
控制器的任务是决策者,以便您出于安全目的在控制器内执行身份验证检查等。
这样想,您不会像在用户前端中那样使用相同的控制器来更改管理中的记录,但您将使用相同的模型。
模型应该在更多地方使用,而不仅仅是前端,因此您不会在模型方法中放置会话检查、输入验证,因为您将根据操作发生的位置执行不同的检查。
您的前端控制器将具有以下内容:
您的管理将具有以下内容:
如果您在模型中放置完全相同的检查,那么您将生成多个模型,无论位置如何,它们都会返回相同的数据。
When it comes down to Ajax under the MVC Framework I usually tend to have each function with a specified keyword such as fetch / set.
something like this
To answer your question: yes
The task of the controller is the decision maker, so that you would perform authentication checks etc within the controller for security purposes.
Think of it this way, you would not use the same controller for changing records in your administration as you would within your users front-end, but you would use the same models.
the models should be used in more places than just the front end, so you would not place a session check, input validation in the model methods as you would perform different checks depending on the location the action is taking place.
Your frontend controller would have something along the lines of:
where as your administration would have:
if you place the very same check in your models then you would have generate several models that would do return the same data regardless of the location.