Answer the question
In order to leave comments, you need to log in
How does the MVC controller work?
Good afternoon, I do not quite understand how mvc works, in particular in yii. The question is - is the controller created for each request separately or does one requester get the same controller as the other requester?
For clarification - let's say I have a controller with 2 actions and some parameters, let's say this (pseudocode):
controller myController
{
private param = 5;
action action1
{
this->param = 9;
this->render(this->param);
}
action action2
{
this->render(this->param);
}
}
Answer the question
In order to leave comments, you need to log in
The responsibility of the controller in MVC:
1. Getting parameters from the view (GET / POST, etc.) and passing them to the model
2. Returning a view with parameters received from the model
3. Validating and filtering parameters in both directions
4. Access control based on the rules laid down in the model
That is, the controller is an intermediary between the view and the model. The controller, if possible, should not contain business logic. A feature view should not call model methods directly, a feature go model should not contain presentation (HTML) mixins and return a view. "If possible" - as it is not always possible/justified in terms of development effort.
General rule: thin controller and thick model.
The specificity of web applications is that a new instance of the application is created for each client request, since HTTP (S) is a stateless protocol.
There is one common controller class for all clients (class myController), but for each request, each client gets its own controller instance (new myController)
no, the second user will get 5 because they are interacting with their own controller instance.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question