Answer the question
In order to leave comments, you need to log in
How to implement in MVC?
Good evening.
I want to understand the meaning of using MVC from a to z in PHP. After looking at the tutorials for a bit, a few questions arose:
Answer the question
In order to leave comments, you need to log in
1) What are ancestor classes usually responsible for in MVC?
For storing basic functions, as an example, let's consider the Controller
. Suppose that the core of the framework always calls three methods, in the following sequence:
beforeAction - run something before the action is executed
action - run the action itself
afterAction - run something after the action is executed
* * action = controller class method
so that you don't have to declare beforeAction and afterAction, they are declared in some abstract Controller class, from which further inheritance occurs and all descendants receive these methods
Or, using the model as an example, there is some ActiveRecord class, you inherit your model from it and all descendants get all its functionality at once: for example, a query constructor through a chain call:
Posts::find()->where(['title'=> 'hello world'])->orderBy(['id'=>SORT_DESC])->all();
well, your model looks like this class Posts extends ActiveRecord{}
i.e. all the logic is hidden in the parent
2) In different ways, here you need to look at a specific framework: it happens that the service container does this, sometimes DI, sometimes the factory is simply called, and inside it is passed a connection that is stored somewhere in the "registry" of the kernel ... in generally differently.
3) Again, in different ways, but in general the principle is the same. Each entity has its own model, for example, there is a separate Contests model, there is a separate Participants model, etc. You need to show separately the number of guys and girls, then in the controller responsible for displaying the competition, you access the Participants model and get the necessary information (something like $boys = Paticipant->maleCount(); $girls = Paticipant->femaleCount (); ) and pass this data to view. And the calculation itself, working with the database, etc. goes already in the model (Paticipant). As a result, it turns out that all your logic is divided into the following steps:
- the core analyzes the request and calls the controller
action - the controller action receives the necessary data from the model and passes it to the view
- the model contains any logic for receiving and processing data
- the controller or controller action renders the view and gives the code to the client
4) Well, usually on the website of the framework they write what is included there. All sorts of goodies for developers (for example, helpers for creating forms, classes for working with SSH, console, etc.), tests, documentation and a bunch of all sorts of things that are often never used
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question