D
D
Dom Alfro2019-03-05 14:47:08
Design patterns
Dom Alfro, 2019-03-05 14:47:08

Are we talking about MVC?

Good day ! how do I understand the meaning - when they say that the model should not know anything, neither it controllers nor views, and everyone should not know anything with the eye - the controller is like a conductor - but how do I understand what this means that they should not know anything about each other - be somehow cleverly connected or encapsulated - used through non-imspace - I just saw one code and I really didn’t like it: it was like this: a model was connected to the controller - to use its methods =

include '/models/Category.php';
// this is controller Category 
class Categorycontroller {
    public function get() {
         $set = Category::getCategoryList();
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Talyan, 2019-03-05
@mustang_shelby

class NewsModel {//модель
public function getNews()
{
$news=;//подключаемся к бд, получаем новости, и т.д.
//при этом модель понятия не имеет, что там творится с контроллером и view - модель просто отдаёт из БД новости
return $news;
}
}

class View {
public function render($data)
{
include "view.phtml";//здесь в файле- данные расставляются в теги, крутятся в foreach и т.д. На входе - только данные из контроллера. Сам view ничего делать не умеет, кроме как отображать данные
}
}
class NewsController { //контроллер
public function action_getNews()
{
$news=new NewsModel();
$view=new View();
$view->render($news->getNews());
/*контроллер понятия не имеет, как модель получила новости. Контроллер всего лишь получил запрос экшена - action_getNews, взял из модели данные, и отдал в view. Контроллер - контролировал эти действия, но не изменял ни БД, ничего другого*/ 
}
}

1) In the future, you can change the model, the way to connect to the database, upgrade the functionality.
2) Change the view as you want - the input data comes from the controller, and the controller does not need to be touched.
3) When you change the controller, you change the logic of the application, increasing its functionality.
This allows you to apply DRY - Don't Repeat Yourself. In procedural programming, you make functions in the same way, and then you use them. Why is it worse to connect a separate class (model) and use it in any controller?
You can use the News model both in the Reader Controller to display news and in the Admin Controller to edit the news. You will not again write functions for receiving news for the admin, if the class for working with news already exists, and gives out what is needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question