I need to loop through categories and then the top stores within each of the categories, and being relatively new to MVC I’ve unsure as to how to accomplish this in keeping with the MVC principles.
At the moment I was planning on doing something like this, but now I look at it I see an awful lot of stuff which I feel should be in the model:
My Controller looks like this:
public function category_list() {
foreach ($this->CategoryModel->getCategoryList() as $cat) {
$data['cat_title'] = $cat['category_title'];
$data['list']['stores'] = $this->StoresModel->getStoresByCategory($cat['category_id']);
$this->_Load->view('stores_by_category.tpl', $data);
}
}
Is this the right way around achieving this task or is my feeling of guilt correct?
Many thanks
The good:
You are using the controller to populate the data to be used in the view.
You are making a model call to getCategoryList() vs. native sql, etc.
The bad:
Wrapping the template call within the loop. You should localize the the rows from getCategoryList() into $data like your doing, but then call the template once, with the $data array, then your template will iterate through $data.
Pointers:
Your controller should be as light as possible, however there is some wiggle room here. If you have business logic which dictates which categories to return, you need to determine if the logic will be accessed from multiple code points. If this is the case, I recommend you put the business logic into a centralized location (new class), which will be responsible for pulling in the model data, massaging it according to business rules, then returning the data to the calling code. If it’s a one time business logic rule, then it is safe to put within the controller.