Answer the question
In order to leave comments, you need to log in
Is there a good book on MVC in PHP? Not a dry theory, but a real (not very complicated) example?
I thought "PHP. Objects, patterns and programming techniques" would help me, but it's purely theory, then I started reading Pitt C. - Pro PHP MVC (Expert's Voice in Open Source) but everything is too complicated there. It would be great if there is some great book on this subject. Thanks in advance.
Answer the question
In order to leave comments, you need to log in
Everything is easier. Don't get confused.
There is such a GRASP pattern, a controller that processes requests. Let's add a model there (the entity, the container for the data that our business logic operates on, has nothing to do with database-type storages) as a data presentation layer inside the application and a presentation layer that converts models into what the application should give at the output. And these are not necessarily templates, it can be serialization, marshaling, or something else.
To get the simplest example, instead of a database, you need to take the usual SplObjectStorage, and just write some very simple application that will not necessarily work via http, but simply be emulated.
And that's all. It is better to drive on the topic of thin controllers and service-oriented architecture, SOLID, GRASP, GoF, etc.
And even better - write code, cover it with tests and refactor. So you will understand much more effectively why it is so and not otherwise, and why it is so bad and so good.
When I started to program sites, and the 4th PHP was almost a curiosity, there was already a problem to separate the site code, pictures and html. Well, CSS at the same time to pictures. The designer was given a separate FTP account, and he accidentally deleted something there.
We then separated the HTML into a separate folder, the logic into its own folder. We agreed that there would be no HTML in the logic. Now they will call it a "bicycle", but this was the beginning of the 2000s. We split the site in half. One of the first frameworks, CodeIgniter, was built using this system. It was made under the 4th PHP, without classes, "anyhow" worked. The one who brought him, probably does not know what a bad example it is. Because, as in our case, he did not have a full division into three layers. It has a model, logic, but its presentation layer is rather weak.
Real MVC is one that has separate work with data (model), logic (this is what, as written above, accepts a request from the client and returns a response to it) and presentation. Now, of course, separate aki for FTP do not rule the ball, now VCS (although someone interferes), but as before, it's all in separate folders, and in each folder - if not its own repository, then at least its own VCS branch. There may be other layers, service, table level, etc., this is who calls it what and who has what approach, but there are never less than three of them.
I started learning MVC with the development of Zend, and still consider this implementation one of the best. Another good implementation (maybe not the most beautiful of course) is the Yii framework.
I advise you to put a demo of one of these systems, arm yourself with lectures and articles on frameworks, and experiment. And to know, as they wrote here, that the main thing here is "why", and not "how".
Take any FrameWork and read about its device.
For example
www.code-igniter.ru
https://ellislab.com/codeigniter
You make an elephant out of a fly. The problems are for two or three classes.
The word "logic" further means "work with the database, selections, business logic".
There are only three layers in the MVC design pattern: Model + Control + Display. It is obvious that the Mapping cannot contain logic in your understanding. Therefore, it is implemented either in the Model or in the Control. Either "thick controllers" or "thick models" are obtained.
If you are so worried about the "ideal model", where the logic is not in the models, and not in the controller, then more layers of abstraction are needed.
For example, an abstraction is introduced that deals only with selections - the Data Provider. It usually operates with one sample for a specific purpose. Its task is to build a query to the database using one or more Models, using a minimum set of input parameters passed from the Controller, and or otherwise prepare a list of data. To unify work with repetitive data, the Drid abstraction is introduced. The Controller selects the type of Grid, Data Provider, passes to the Provider parameters that affect the selection, and to the Grid - to display. The Grid has its own View, and is embedded in the view that is associated with the Controller.
For storing business logic and validation rules, a Model is generally suitable, but you can introduce the Form abstraction. The form must validate the data of one or more Models for a specific purpose. At the same time, the business logic can concern several loosely coupled application modules, and the connections can be implemented through the event mechanism, where the modules subscribe to the events of the Form or Models. The controller in this case serves to select the form and transfer the data received from the user to it. The form has its own View, and is embedded in the view that is associated with the Controller.
Here is the page having the form: https://www.dropbox.com/s/qn86upnl1oc1viu/Controll...
Here is the page having the list: https://www.dropbox.com/s/uxvk5uycmop8ddu/Controll...
I apologize for the small print - I did it on my knees :)
Of the implementations of such abstractions, I liked the Yii framework.
Recently I got to a very good video tutorial on this issue, in several parts they create a site on an mvc template.
Video list: jream.com/lab
First video tutorial: youtu.be/Aw28-krO7ZM
What about articles that don't work for you? On Habré, for example, there are good guides with examples.
www.ozon.ru/context/detail/id/19665827
here is a clear, simple, understandable and very useful literature there, as well as templates and techniques with simple examples. I advise.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question