Answer the question
In order to leave comments, you need to log in
Why are there exactly 3 components in the MVC design pattern?
I understand why they allocate a separate View.
1. When changing the interface (but not the logic), the classes responsible for the application logic are not affected
2. It is impossible to write tests for the GUI, therefore, by separating the code responsible for the user interface into separate classes, you can apply tests to the Model and Controller
3. In theory, we simplify porting the application to another GUI framework, since only the View will have to be rewritten.
But I don’t understand why the Model and Controller should be separated.
After all, the rest of the application is some data plus an interface to them. This fits perfectly into the OOP paradigm. Data is private members of a class, interface is public. You can read and change data only through the Controller. The data is Model.
What am I doing wrong? What will I face if Model and Controller are merged into one entity. What will I get if I try to separate the Model and Controller into different entities that will interact through a certain interface? But how do you share them?
Answer the question
In order to leave comments, you need to log in
What you are describing is the fat controller approach. Discover the wonderful world of Service-Oriented Architecture . In general, as models, they are also domain models, this is just a representation of data. For data storage and processing, validation, etc. services respond. Controllers can also be represented as services, and the task will only be to check whether the user has access to this operation, what actually needs to be done, etc. Thus, the controller may have no idea at all about where the data is stored, how it is processed ... It just looks that the user can save data, passes the user's data to the service, it saves ....
In short, something like this.
Model example:
class User
{
private $id;
private $email;
private $password;
private $enabled;
public function getId() {...}
public function getEmail() {...}
public function setEmail($email) {...}
public function getPassword() {...}
// за хэширование пароля отвечает сервис, модель ничего об этом знать не должна.
public function setPassword($password) {...}
public function isActive() {}
public function activate() {}
}
/**
* @Route("/users")
* @Method("POST")
*/
public function createUserAction(Request $request) {
if (!$this->get('security.context')->isGranted('ROLE_ADMIN')) {
throw new AccessDeniedException();
}
$user = new User();
$form = $this->createForm(new UserForm(), $user);
$form->bindRequest($request);
if (!$form->isValid()) {
// save errors to session, redirect
return $this->redirecti(...);
}
$user = $form->getData();
$this->get('app.user_manager')->createUser($user);
return $this->redirect();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question