A
A
AllReady2015-11-14 18:54:29
PHP
AllReady, 2015-11-14 18:54:29

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:

  1. Usually models, controllers and views inherit from their common class (Model, Controller, View). What are ancestor classes usually responsible for in MVC.
  2. Regarding the first question, about the model. For example, we have a database connection class (is this done in the Model class?) How do you usually pass a database connection object in a model? Does each method use something like parent::__construct ?
  3. There is let's say a module with photo contests. In the procedural style, a lot of requests come out and are stupidly inserted on one page (for example, the number of contests, the number of participants, the number of guys, the number of girls, the number of new photo contests, etc.). How is this implemented in MVC? Similar ? Simply we prepare these requests in the model? And we pass it to the view?
  4. And the most important question, for example, when downloading frameworks like laravel, yii, these frameworks weigh 15-30mb each. What goes there for 15-30 mb, I can’t understand? What is usually included in frameworks to achieve this size?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaliy Orlov, 2015-11-14
@AllReady

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 question

Ask a Question

731 491 924 answers to any question