php - MVC : what code belongs to the model

标签 php model-view-controller cakephp

几周以来我就开始开发 CakePHP 项目。从一开始我就一直在为 Controller 内的代码量而苦苦挣扎。在大多数情况下, Controller 的代码行数比模型多。通过了解“瘦 Controller ,胖模型”这一表达方式,我现在正在寻找一种在模型中放入更多代码的方法。

此时出现的问题是“在哪里划清界限”。 Controller 应该做什么,模型应该做什么。已经有一些问题/答案,只是我正在寻找更实用的解释。例如,我在下面放置了一个函数,该函数现在位于 Controller 内部。我认为该代码的一部分必须并且可以移至模型中。所以我的问题是:我可以将哪些部分移至模型中,哪些部分可以保留在 Controller 中。

/**
* Save the newly added contacts and family members.
*/
public function complete_contacts()
{
    if ($this->request->is('post')) {
        if (isset($this->data['FamilyMembers'])) { 

            $selected_user = $this->Session->read('selected_user');

            $family_members = $this->data['FamilyMembers'];

            $this->ContactsConnection->create();
            foreach ($family_members as $family_member) { 
                // connection from current user to new user
                $family_member['ContactsConnection']['contact_family_member_id'] = $selected_user['id'];
                $family_member['ContactsConnection']['nickname'] = $selected_user['first_name'];
                $this->ContactsConnection->saveAll($family_member);

                // inverted connection from new user to current user
                $inverted_connection['ContactsConnection']['family_member_id'] = $selected_user['id'];
                $inverted_connection['ContactsConnection']['contact_family_member_id'] = $this->FamilyMember->inserted_id;
                $inverted_connection['ContactsConnection']['nickname'] = $family_member['FamilyMember']['nickname'];
                $this->ContactsConnection->saveAll($inverted_connection);
            } 
        }
    }
}

我应该在 FamilyMember 模型中创建一个名为“save_new_family_member($family_member, $selected_user)”的函数吗?

最佳答案

就M和C的目的而言

The model manages the behavior and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller).

The controller receives user input and initiates a response by making calls on model objects. A controller accepts input from the user and instructs the model and a view port to perform actions based on that input.

我建议你可以通过

    $selected_user = $this->Session->read('selected_user');

到您的模型并为模型的每个内部执行您的操作。您可能想要更改有关数据存储方式或对其执行某些转换的规则,而 Controller 应该对此视而不见。基本上使用 Controller 将信息[经常从 View ]获取到模型。不要直接从 Controller 操作模型。简而言之,是,创建您建议的函数:)

话虽这么说,有时我发现自己的 Controller 必须做的事情比我想要的更多,在这种情况下,至少将任务分解为辅助方法,这样你的 Controller 更易于管理,并且你可以重用代码在需要的地方。

关于php - MVC : what code belongs to the model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9456365/

相关文章:

c# - 返回带有查询字符串的 View

model-view-controller - 在 MVC 中使用数据注释时,使用接口(interface)与 MetadataType 的优点和缺点

php - 使用 CakePHP,您如何在用户登录时隐藏页面的某些元素?

php - Laravel 如何仅响应没有正文消息的 204 代码状态

php - 如何使用 IIS 在 Windows Server 2008 上安装 Laravel 4.2/5?

c# - 如何做Json表单MVC模型

cakephp - 在 cakephp View flie (.ctp) 上更改,但在浏览器中没有更改

php - 克隆实体和 CakePHP 3 中的所有相关实体

javascript - 如何将 JSON 对象解析为 mysql 查询

php - 在 PHP 中追加文件...无法换行 (\n) 来处理变量