php - 条件语句是否属于 php mvc 中的模型或 Controller ?

标签 php oop model-view-controller class

如果您正在使用 mvc 来构建用户配置文件,那么在模型或 Controller 中的函数中使用条件语句来计算评论的显示类型会更好吗:

例如 我有 3 个类(class)

  • 评论
  • 成员
  • 管理员(扩展成员)

一些示例使用缺少函数的伪代码

选项 1

根据返回评论的 showComments 函数中登录的用户类型,将返回不同的信息。

class user {

    function isLoggedIn() { //Check if a user is logged in }

    function getUserType() { // return user type }

    function showComments($id) { //comments code }
}

class admin extends user {
    function showComments($id) { //comments code }
}

然后使用 Controller 中的代码根据登录的用户类型确定要显示的内容?

$profileContent = $user->getOtherContent();

if ($user->isLoggedIn() && $user->getUserType() == "member") {
    $member = new member();
    $comments = $member->showComments($profileId);
}
elseif ($user->isLoggedIn() && $user->getUserType() == "admin") {
    $admin = new admin();
    $comments = $admin->showComments($profileId);
}
else
    $comments = $user->showComments($profileId);

require 'templates/profile.php';

选项 2

由于这是一个自定义框架,我可以将所有内容移动到模型中的一个函数中,并在用户中使用一个函数来检查要显示的评论类型:

abstract class user {

    function isLoggedIn() { //Check if a user is logged in }

    function getUserType() { // return user type }

}

class profile {

    function showComments($profileId, $user) {

        if (User::isLoggedIn() && User::getUserType() == "member") {
            $comments = //database query and formatting for member
        }
        elseif (User::isLoggedIn() && User::getUserType() == "admin") {
            $comments = //database query and formatting for admin
        }
        else
           $comments = //database query and formatting for guest

        return $comments;
    }
}

使用如下 Controller :

$profile = new profile($profileId);
$comments = $profile->showComments();

require 'templates/profile.php';

最佳答案

从技术上讲,两者都是正确的。 MVC 模式是有意抽象的,关于模型和 Controller 的正确域是什么存在一些争论。

根据您使用的确切框架,可能有一个“更好”的答案。否则,做您认为最有意义的事情。

更新 - 鉴于您的问题发生了变化,我想稍微调整一下我的回答:

对于选项 1,这样设计模型会更有意义:

class user {
    function isLoggedIn() {}
    function getUserType() {}
    function showComments() {}
}

class admin extends user {
    function getUserType() {}
    function showComments() {}
}

class member extends user {
    function getUserType() {}
    function showComments() {}
}

在 Controller 中,$user 应该被实例化为管理员、成员或用户(这可以通过静态工厂或直接在 Controller 中完成)。之后你的 Controller 就是

if ($user->isLoggedIn()) {
    $comments = $user->showComments($profileId);
}

(为了让它更智能,可以将 profileId 设置为类属性(除非用户有多个配置文件?)

选项 2,otoh,是对模型到模型设计的巧妙运用。

我看到的唯一真正区别是您如何将评论概念化。您是否将他们视为用户的一部分(与个人资料的联系较弱)?或个人资料的一部分(与用户关系较弱)?这两种方法都没有任何我立即看到的特别痛点,因此最好的选择是使用对您来说最有意义的方法。

关于php - 条件语句是否属于 php mvc 中的模型或 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8824689/

相关文章:

php - 要求作者为帖子设置特色图片

php - 无法在我的 Windows 10 PC PHP 版本 8.0.3、Laravel 版本 8 上安装 spatie laravel-backup 包

C++ - 使用基类作为模板参数

ruby-on-rails - rails : Creating an index view for a devise user

php - Eloquent where 和具体条件有关系怎么实现?

c++ - 重载构造函数中的代码重复

java - Java中是否有必要在每个类中都有一个构造函数

model-view-controller - ASP.NET MVC 的下一个完整发布日期(MVC 1.1/1.5/2.0 有人吗?)

javascript - 当我在文本框中输入值并点击提交时,我希望使用 ajax 在文本框下方输入该值

javascript - 在不刷新页面的情况下更新 DataTables 表中的行