I am writing a CMS for my needs. It is built on MVC, there are main -> controller, model and view ( view ) them (controller model) inherit already index_controller , index_model and so on.

The question is as follows: on some pages, for example, www.web.com/static and say www.web.com/newslist , I need to display a list of news on the right.

Will it be correct to write a method that executes this sql query in the main model.php model (since this model is always loaded) and, if necessary, display a list, or do you need to write this method in the model of each individual controller ( static_model.php and newslis_model.php ) code duplication ?!

  • I also advise you to read Dependency Injection - E_p

2 answers 2

MVC Great idea but simplified, incomplete and leaves a lot to interpret. Concrete is only for the species. Still can not decide where there should be more logic in the model or controller. And in large applications, a large piece of logic can logically not belong to either the model or the controller.

And if on the issue then:

  1. The essence of OOP applications is that each class performs one function (not to be confused with function () {} ). Therefore, the general class can not pull the news.

  2. In the classical sense, the модель = данные + бизнес логика + валидация , which in the context of the question can be simplified as a модель = данные . So read best in the news model.

Example (for using static methods slippers do not throw at me).

 // models/news.php <?php namespace Models; // Class extends and autoloads models/base.php; class News extends Base { // } // controllers/c1.php <?php namespace Controllers; // autoload and include models/news.php use Models\News; // Class extends and autoloads controllers/base.php; class C1 extends Base { public function indexAction($params) { // Validation $news_list = News::getList($params['smth']); //or $news_list = $this->_modelFactory('News', $params['smth']); // View } } // controllers/c2.php <?php namespace Controllers; // autoload and include models/news.php use Models\News; // Class extends and autoloads controllers/base.php; class C2 extends Base { public function otherAction($params) { // Validation $news_list = News::getList($params['smth']); //or $news_list = $this->_modelFactory('News', $params['smth']); // View } } 
  • If at least a little deeper into MVC, then the модель != данные . Otherwise it is Anemic Domain Model with all that it implies. Well, Active Record is not the best solution (although acceptable in a number of cases). - Dmitriy Simushev
  • @DmitriySimushev I completely agree. The main thing is that the correct answer may vary from the question (details). Also, the author has no knowledge to make a decision. Therefore, I think it makes sense to simplify the answer, and then with experience the author will understand what, where and how. - E_p
  • @DmitriySimushev Corrected the answer. I hope so clearer and more correct. - E_p
  • Yes, it became a little more accurate. However, validation is an integral part of the model / business logic (but here I’m already finding fault =)) - Dmitriy Simushev
  • Due to the lack of knowledge of OOP, I probably did not quite correctly put it, I meant that there is a main controller class and all other controllers (for example, the Index controller) are already submerged through the router and extend the main class index extends controller , as happens with models 'index_model extends model' - Cone Enoc

not duplication but modularity you have to build a system so that these are separate modules. static_model.php is for example the model of the table static and accordingly newslis_model.php is respectively newslis , etc. then for each view some block is generated, and at the end of these blocks there is a page. This is necessary in order to:

  1. you could change something (delete, disable) and the application did not go wild with it
  2. You can display this block on any other page.