Sobes.tech
Junior — Middle

Could you explain the role of the controller in the MVC architecture?

sobes.tech AI

Answer from AI

In MVC (Model-View-Controller) architecture, the controller acts as an intermediary between the model and the view. Its main tasks are:

  • Receiving and processing incoming requests from the user.
  • Calling the appropriate model methods to handle data.
  • Selecting and passing data to the view for display.

The controller does not contain business logic or display logic but coordinates the interaction between the model and the view, ensuring separation of concerns and simplifying code maintenance.

Example in PHP:

class UserController {
    public function showProfile($userId) {
        $userModel = new UserModel();
        $userData = $userModel->getUserById($userId);
        include 'views/userProfile.php';
    }
}