How can the Controller, Model, and View components interact in a PHP MVC framework to maintain separation of concerns?
In a PHP MVC framework, the Controller, Model, and View components interact by following the separation of concerns principle. The Controller receives user input, processes it, and interacts with the Model to retrieve and update data. The Model represents the data, business logic, and database interactions. The View is responsible for presenting the data to the user in a user-friendly format. To maintain separation of concerns, the Controller should not directly interact with the View or Model, and the Model should not have any knowledge of the View.
// Controller
class Controller {
public function index() {
$model = new Model();
$data = $model->getData();
$view = new View();
$view->render($data);
}
}
// Model
class Model {
public function getData() {
// Database query to retrieve data
return $data;
}
}
// View
class View {
public function render($data) {
// Display data to the user
}
}
// Usage
$controller = new Controller();
$controller->index();