PHP MVC - Is my controller too fat? -


i think put code in controller supposed go in model.

this part of controller, i'm not gonna paste since there 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] $key => $post) {                 if ($post != "" && $key != "reg_date") {                     $searchterms = explode(' ', $post);                     foreach ($searchterms $term) {                         $term = trim($term);                         if (!empty($term)) {                             $like[] = $key." '%".trim($term, '\'')."%'";                         }                     }                  }                 else if ($post != "" && $key == "reg_date") {                     foreach ($post[0] $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(' , ', $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 model database queries:

  public function getusers() {     $data = $this->_db->select("select * ".prefix."users" . $this->_sort);     return $data; }  public function countusers() {     $data = $this->_db->select("select count(*) id ".prefix."users");     return $data[0]->id; }  public function filterusers($like_clause) {     $data = $this->_db->select("select * ".prefix."users " .$like_clause. $this->_sort);      return $data; } public function countfilterusers($like_clause) {     $data = $this->_db->select("select count(*) id ".prefix."users ".$like_clause);     return $data[0]->id; }  

should moved foreach loops in model?

php mvc - controller fat?

yeah, because contains business logic, therefore no separation of concerns. , reason because don't implement model correctly. model consists of data mappers , domain logic handlers. , things bring data mappers , domain objects called services

a model not class. calling model class calling class myliskovsubstitionclass {}. that's concept of data asbtraction. model consists of services.

to implement correctly, you'd start writing mapper:

class usermapper {   public function getusers()   {     $data = $this->_db->select("select * ".prefix."users" . $this->_sort);     return $data;   }   // ... rest abstracts table access } 

and you'd write service, 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();               // rest            }             return $data;       } } 

and :

public function ajaxusers() {     if ($_get["action"] == "listusers") {        $result = $this->usermanager->search($_post);       die(jscon_encode($result));     } } 

Popular posts from this blog