As I see, in the vast majority of PHP frameworks working on the MVC paradigm, all controllers, views and models are made as classes. Do you think it is necessary for a good application?
For example, I use classes for all models (although they are not really that useful everywhere), but controllers and views are not classes. The controller is just a script, the presentation is also just a template (on Blitz'e), well, for it there is a piece of processing any cycles and display conditions in the controller (as if by a view controller). Although, I have not yet experienced and did not know the zen of the PLO, but still, if I don’t get any benefit with the class for C and V, is this normal?
4 answers
The main idea of MVC is to divide all logic into three separate components, respectively, if you, as far as possible, execute this “simple” condition, your application adheres to the MVC paradigm, regardless of whether you use OOP or not.
about
"Only classes will give extensible and reusable code"
"really flexible code is OOP"
The flexibility of the code is almost 100% dependent on the programmer and I would argue with you very strongly in this matter. Yes, of course, the PLO is a great tool, which is sometimes simply vital, but, at the same time, it is often used by someone else, like everywhere else.
In general, I catch stones, but - not OOP makes the code flexible and good, but a programmer, you should never forget about it.
Only classes will give extensible and reusable code ... so learn and come with you happiness;)
Well, actually, really flexible code is OOP. You will encounter this when you saw, something big)
- Now I thought carefully and understood - indeed, in my case, only at first it seems that classes are not needed for controllers. But if you think about the need to change something big, as you said, such as a template engine, then you will have a fig. - Oleg Arkhipov
In general, in the MVC pattern, there must necessarily be only one class - a router that processes URLs and delivers content based on that URL itself. A bouquet of other classes is your choice. Here only a question about good style, convenience and portability of the code.
- fourI wonder why
MVC
then stands forModel-View-Controller
, notRouter
. - Costantino Rupert - four@Asen - "Details" will not fit in the comments, so read
GoF
:) - In the fundamentalMVC
pattern there is no mention of routers andurl'ах
. Especially sinceMVC,
for example, can be used to build the architecture of a desktop application, respectively, theURL
is completely out of the question here. - Costantino Rupert - 3@Asen I didn’t quite understand the phrase about the fact that nevertheless
MVC
remains the way it was, but oh well :) - Costantino Rupert - one@Construct - if you are talking about a classic of the type index.php, to which all requests come, then this template is called the "front controller" - Zowie
- one@Construct is something like that :) FrontController is just a centralized entry point to your application. Router is what FC should call to determine the future fate of a specific request :) Therefore, you also have a router in one form or another, if you are talking about it. - Zowie
C
andV
:) - Costantino RupertMVC,
with the exception of the division of areas of responsibility, is the simplicity of testing. - Models with their factories can be easily tested separately from theController / View.
logicController / View.
- And then (if the model is well written), then it is triviallymock'ается
and usually you can immediately write a pack of decent tests on the controller, or even write them before the controller itself, followingTDD.
- TestingView
usually either very hard or just pointless, so the correctView
should be minimal in terms of the amount of logic in it. - Costantino Rupert