It's kind of hard to define what is right or wrong at conceptual discussions such as this one. All I can provide you is my own limited experience and opinion. Absorb it with your own opinion and decides what's best for you in your own opinion. Build your first architecture, implement your first project and then your own project will start to tell you if your architecture is working for you or not. Here is a few personal tips.
- I never output anything to the browser from any layer except the VIEW.
That is, if you're moving towards MVC model, try to keep the connection between the server and the response to a single layer, which seems to be a perfect job for the VIEW layer, that handles anything from and to what the user is seing (User Interface).
- Learn addicional design pattern to complete your archtecture
In my opinion, MVC is not a fully working concept/archtecture. That is, it perfectly defines key responsabilities, but doesn't define how to make everything work. For instance, how will you define your model? You can go with Active Record (like your code represents) or you can opt out for DAO (Data Access Object). In a few words, the differences would be like this:
class ActiveRecordModel {
protected function connect();
protected function create();
protected function update();
protected function delete();
}
class Car extends ActiveRecordModel {
public $id;
public $maker;
public $model;
public $year;
}
This way, you can use $myCar->create();
and the model will be created inside the database. As oppose to DAO
class Car {
private $id;
private $maker;
private $model;
private $year;
// Getters, Setters, Constructor
}
class CarDAO {
public function add(Car $obj){
// Establish connection here or at the constructor
// Take data from $obj and arrange it to be stored.
}
}
-
Controllers talk to Controllers, but only one Model
I usually prefer making one controller for each model and that controller will know how to handle that model (regardless of which design pattern the model is using). That being said, I avoid using more than 1 model inside one controller. If you need to access data from a model that is not your own, talk to it's controller instead of bypassing the controller and going direct to the model. Here is what I mean:
class AccountController extends GenericController {
public function add($name, $phone, $birthday, $email, $password) {
$accountModel = new Account();
$accountModel->name = $name;
$accountModel->phone = $phone;
$accountModel->birthday = $birthday;
$accountModel->password = $password;
$id = $accountModel->insert();
$emailController = new EmailController();
$emailController->add($email, $id);
// Here, instead of declaring a model for "Email",
// I'm declaring the controller
// so, in my architecture, whenever I need to change some rule
// for some entity, I know I only have to work at one single controller.
}
}