CodeIgniter 控制器和 AJAX

发布于 2024-10-16 14:44:23 字数 415 浏览 0 评论 0原文

编辑:上一个标题,因为没有人读这个问题:“如果我在 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 技术交流群。

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

发布评论

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

评论(1

最单纯的乌龟 2024-10-23 14:44:23

当谈到 MVC 框架下的 Ajax 时,我通常倾向于让每个函数都带有指定的关键字,例如 fetch/set。
像这样

class Users extends Controller
{
    public function Fetch($id){}
    public function Create(){}
    public function Update($id){}
    public function Remove($id){}
}

回答您的问题:

控制器的任务是决策者,以便您出于安全目的在控制器内执行身份验证检查等。

这样想,您不会像在用户前端中那样使用相同的控制器来更改管理中的记录,但您将使用相同的模型。

模型应该在更多地方使用,而不仅仅是前端,因此您不会在模型方法中放置会话检查、输入验证,因为您将根据操作发生的位置执行不同的检查。

您的前端控制器将具有以下内容:

public function Fetch($id)
{
    if($this->session->get_userdata("auth_level") & USER_AUTH_LEVEL_READ)
    {
        //Show data
    }
}

您的管理将具有以下内容:

public function Fetch($id)
{
    if($this->session->get_userdata("auth_level") & IS_ADMINISTRATOR)
    {
        //Show data
    }
}

如果您在模型中放置完全相同的检查,那么您将生成多个模型,无论位置如何,它们都会返回相同的数据。

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

class Users extends Controller
{
    public function Fetch($id){}
    public function Create(){}
    public function Update($id){}
    public function Remove($id){}
}

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:

public function Fetch($id)
{
    if($this->session->get_userdata("auth_level") & USER_AUTH_LEVEL_READ)
    {
        //Show data
    }
}

where as your administration would have:

public function Fetch($id)
{
    if($this->session->get_userdata("auth_level") & IS_ADMINISTRATOR)
    {
        //Show data
    }
}

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.

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