doupingdiao3546 2015-04-09 21:28
浏览 52
已采纳

PHP MVC - 我的控制器太胖吗?

I think I put too much code in my controller that was supposed to go in the model.

This is a part of my controller, I'm not gonna paste everything since there is a lot of code.

  public function ajaxUsers() {
    if($_GET["action"] == "listUsers") {

        if(!isset($_POST["search"])) {
            $this->_data['Records'] = $this->_model->getUsers();
            $this->_data['Result'] = "OK";
            $this->_data['TotalRecordCount'] = $this->_model->countUsers();
        }
        else {
            foreach($_POST['fields'][0] as $key => $post) {
                if ($post != "" && $key != "reg_date") {
                    $searchTerms = explode(' ', $post);
                    foreach ($searchTerms as $term) {
                        $term = trim($term);
                        if (!empty($term)) {
                            $like[] = $key." LIKE '%".trim($term, '\'')."%'";
                        }
                    }

                }
                else if ($post != "" && $key == "reg_date") {
                    foreach ($post[0] as $key2 => $date) {
                        $datetofrom = strtotime($date);
                        $datetofrom = date('Y-m-d', $datetofrom);
                        if ($date != "" && $key2 == "datefrom") {
                            $like[] = "DATE_FORMAT(".$key.", '%Y-%m-%d') >= '".$datetofrom."'";

                        }
                        if ($date != "" && $key2 == "dateto") {
                            $like[] = "DATE_FORMAT(".$key.", '%Y-%m-%d') <= '".$datetofrom."'";
                        }
                    }
                }
            }

            ($like) ? $where_clause = "WHERE ". implode(' AND ', $like) : $where_clause = "";   
            $this->_data['Records'] = $this->_model->filterUsers($where_clause);
            $this->_data['Result'] = "OK";
            $this->_data['TotalRecordCount'] = $this->_model->countfilterUsers($where_clause);
        }
        echo json_encode ($this->_data);
    }
}

And my model is mostly database queries:

  public function getUsers() {
    $data = $this->_db->select("SELECT * FROM ".PREFIX."users" . $this->_sort);
    return $data;
}

public function countUsers() {
    $data = $this->_db->select("SELECT count(*) as id FROM ".PREFIX."users");
    return $data[0]->id;
}

public function filterUsers($like_clause) {
    $data = $this->_db->select("SELECT * FROM ".PREFIX."users " .$like_clause. $this->_sort); 
    return $data;
}
public function countFilterUsers($like_clause) {
    $data = $this->_db->select("SELECT count(*) as id FROM ".PREFIX."users ".$like_clause);
    return $data[0]->id;
} 

Should I moved the foreach loops in the model?

  • 写回答

1条回答 默认 最新

  • douzi1350 2015-04-09 23:27
    关注

    PHP MVC - Is my controller too fat?

    Yeah, especially because it contains business logic, therefore no separation of concerns. And the reason is because you don't implement a model correctly. A model consists of data mappers and domain logic handlers. And things that bring data mappers and domain objects are called Services

    A model is not a class. Calling model a class is like calling class MyLiskovSubstitionClass {}. That's a concept of data asbtraction. Model consists of services.

    To implement it correctly, you'd start from writing a mapper:

    class UserMapper
    {
      public function getUsers()
      {
        $data = $this->_db->select("SELECT * FROM ".PREFIX."users" . $this->_sort);
        return $data;
      }
      // ... The rest what abstracts table access
    }
    

    And then you'd write a service, which is called UserManager

    final class UserManager
    {
          private $userMapper;
    
          public function __construct($userMapper)
          {
              $this->userMapper = $userMapper;
          }
    
          public function search(array $input)
          {
               $data = array();
    
               if (!isset($input["search"])) {
    
                 $data['Records'] = $this->userMapper->getUsers();
                 $data['Result'] = "OK";
                 $data['TotalRecordCount'] = $this->userMapper->countUsers();
    
                 // Do the rest
               }
    
               return $data;
          }
    }
    

    And finally :

    public function ajaxUsers()
    {
        if ($_GET["action"] == "listUsers") { 
          $result = $this->userManager->search($_POST);
          die(jscon_encode($result));
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面