在 PHP MVC 中创建方法函数的最佳实践是什么?

发布于 2024-10-21 07:35:22 字数 1671 浏览 1 评论 0原文

我想知道将信息从控制器传递到模型时什么被认为是最佳实践。更具体地说,我正在用户类中创建一个用户注册模型,该模型要求提供某些信息,例如电子邮件、名称和密码。

我想知道是否最好将参数放在模型函数中并以这种方式传递它们,或者是否最好只调用该函数并使用 $_POST 变量进行查询。

这是我提到的两个例子。

方法 1

function register(){

    $first_name = $this->input->post('first_name');
    $last_name = $this->input->post('last_name');
    $email = $this->input->post('email');
    $password = $this->input->post('password_1');

    $this->user_model->register_user($email, $password, $first_name, $last_name));}

function register_user($email, $password, $first_name, $last_name){
    $sql = "INSERT INTO users (user_id, email, passwd, first_name, last_name, registration_date, confirmed, confirmation_code, banned)VALUES (NULL, ?, ?, ?, ?, '".date('Y-m-d')."', 'no', '1fg455675', 'no')";
    $register = $this->db->query($sql, array($email, $password, $first_name, $last_name));
    return $register;
}

方法 2

function register(){
    $this->user_model->register_user());    
}

function register_user(){

    $first_name = $this->input->post('first_name');
    $last_name = $this->input->post('last_name');
    $email = $this->input->post('email');
    $password = $this->input->post('password_1');


    $sql = "INSERT INTO users (user_id, email, passwd, first_name, last_name, registration_date, confirmed, confirmation_code, banned)VALUES (NULL, ?, ?, ?, ?, '".date('Y-m-d')."', 'no', '1fg455675', 'no')";
    $register = $this->db->query($sql, array($email, $password, $first_name, $last_name));
    return $register;
}

我已经删除了很多验证代码以及其他内容,以简化问题,希望您能明白。

I am wondering what is considered best practice when passing info from a controller to a model. More specifically, I am creating a user registration model within a user class that asks for certain info such as email, name, and password.

I am wondering if it is better to put parameters within the model function and pass them that way or if is better to just call the function and use the $_POST variables for the query.

Here are the two examples I am referring to.

Method 1

function register(){

    $first_name = $this->input->post('first_name');
    $last_name = $this->input->post('last_name');
    $email = $this->input->post('email');
    $password = $this->input->post('password_1');

    $this->user_model->register_user($email, $password, $first_name, $last_name));}

function register_user($email, $password, $first_name, $last_name){
    $sql = "INSERT INTO users (user_id, email, passwd, first_name, last_name, registration_date, confirmed, confirmation_code, banned)VALUES (NULL, ?, ?, ?, ?, '".date('Y-m-d')."', 'no', '1fg455675', 'no')";
    $register = $this->db->query($sql, array($email, $password, $first_name, $last_name));
    return $register;
}

Method 2

function register(){
    $this->user_model->register_user());    
}

function register_user(){

    $first_name = $this->input->post('first_name');
    $last_name = $this->input->post('last_name');
    $email = $this->input->post('email');
    $password = $this->input->post('password_1');


    $sql = "INSERT INTO users (user_id, email, passwd, first_name, last_name, registration_date, confirmed, confirmation_code, banned)VALUES (NULL, ?, ?, ?, ?, '".date('Y-m-d')."', 'no', '1fg455675', 'no')";
    $register = $this->db->query($sql, array($email, $password, $first_name, $last_name));
    return $register;
}

I have removed a lot of the validation code and what not to simplify the matter so hopefully you get the idea.

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

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

发布评论

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

评论(2

咋地 2024-10-28 07:35:22

您不应该从模型访问 POST 变量。这将使您的模型的可重用性降低,因为现在它们依赖于 POST 数据来工作。例如,在其他时候,如果您需要一种方法来做同样的事情,但您是从其他来源(CSV)获取数据,您将无法使用相同的模型,因为您已将其与邮政。

当您使用这样的结构时,尝试将它们解耦

You shouldn't be accessing your POST variables from your model. This would make your model less reusable since now they rely on POST data to work. For example, at some other point if you needed a method to do the same thing, but you were getting your data from some other source (CSV) you wouldn't be able to use the same model because you've tied it in with POST.

Try to decouple them when you're working with such a structure

北斗星光 2024-10-28 07:35:22

我不会在模型层中使用 $_POST (也不与您的框架等效):这个层不必知道数据来自哪里。

该模型可以从 Web 服务、命令行程序或其他程序中调用,并且必须仍然有效:它不能依赖于发布到应用程序的任何内容。

控制器是被调用的,接收 HTTP 请求的参数(当然是 HTTP 请求的情况);然后它会提取数据,并将其传递到处理该数据的层:模型。

I would not use $_POST (nor its equivalent with your framework) in the Model layer : this one doesn't have to know where the data comes from.

The Model can be called from a webservice, a command-line program, or whatever, and must still work : it must not depend on anything being POSTed to the application.

The Controller is the one being called, receiving the parameters of the HTTP Request (in case of an HTTP request, of course) ; it'll then extract the data, and pass it to the layer that'll work on this data : the Model.

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