MVC Related Materials
You could list the books you read, because most (if not all) php books that deal with MVC are wrong.
If you want to become a better developer, I would recommend you start with an article by Marting Fowler - GUI Architectures . The following are books from the same author, "Enterprise Application Architecture Templates . " Then the next step is to research SOLID principles and understand how to write code that follows the Law of Demeter . This should cover the basics =]
Can I use MVC with PHP?
Not really. At least this is not a classic MVC, as it was specific to Smalltalk .
Instead, in PHP, you have 4 more templates that are aimed at the same goal: MVC Model2, MVP, MVVM, and HMVC. Again, I'm too lazy to write about the differences again, so I'll just get back to the old comment.
What is a model?
The first thing you should understand is that Model in MVC is not a class or object. This is a layer that contains many classes. Basically, a model layer is all layers combined (although the second layer should be called "Object Object Layer" because it contains "Domain Model Objects"). If you want to read a brief summary of what is contained in each part of the model layer, you can try reading this old comment (go to the "side note" section).
Image taken from Fowler's Service Level article.
What do controllers do?
The controller has one important responsibility in MVC (I will talk about the implementation of Model2 here):
Run commands on structures from the model level (services or domain objects) that change the state of these structures.
Usually it has a secondary responsibility: to bind (or otherwise transfer) structures from the model level to the representation, but this becomes a dubious practice if you follow the SRP
Where to put the SQL related code?
The storage and retrieval of information is processed at the data source level and is usually performed as a DataMapper (do not confuse it with ORMs that abuse this name).
Here's how to make it easier to use:
$mapper = $this->mapperFactory->build(Model\Mappers\User::class); $user = $this->entityFactory->build(Model\Entities\User::class); $user->setId(42); $mapper->fetch($user); if ($user->isBanned() && $user->hasBannExpired()){ $user->setStatus(Model\Mappers\User::STATUS_ACTIVE); } $mapper->store($user);
As you can see, in no case does the domain object know that the information from it has been saved. And neither one nor the other, about where you put the data. It can be stored in MySQL or PostgreSQL or some noSQL database. Or maybe clicked on the remote REST API. Or maybe the cartographer was a manner for testing. All you need to do to replace mapper provides this method with a different factory.
Also see these related posts: