3

In a "classical" web MVC - please correct me if I'm wrong:

  • the controller passes the request data received from the "user" (be it browser, console, etc) to the model layer (consisting of domain objects, mappers, repositories, services, etc),
  • the model layer processes it and returns some result data,
  • the view - as specialized class(es) - processes the result data and sends/displays it to the "user".

I would like to ask:

  • Does the controller create the view?
  • Or does the controller receive the view as a dependency?
  • Or are the controller and the view created completely separately, on the front controller level (let's say in index.php)?

Thank you.

PajuranCodes
  • 303
  • 3
  • 12
  • 43

2 Answers2

3

Your definitions of MVC in generally is right, here is answer for your ask:

Controllers are not responsible for rendering the interface, nor for presentation logic. Controllers do not display anything. Instead, each controller's method deals with different user's request. It extracts the data from said request and passes it to model layer and the associated view.

Decisions about what and how to display are in purview of views. Views contain the presentation logic in MVC pattern. In the context of web applications, views create the response. They can compose a from from multiple templates or just send a single HTTP header.

Controllers can signal the associated view by passing some specific values of the request to that view, but most of the decisions in the view are based on information that the view requested from different services in the model layer.

A Controller's methods are based on what type of requests a user can send. For example in a authentication form it might be: GET /login and/or POST /login.

Source: Controllers, tereško


Classic correct MVC class structure:

Classic MVC class structure

Easy definition:

Model. The model manages the behavior and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller).

View. The view manages the display of information.

Controller. The controller interprets the mouse and keyboard inputs from the user, informing the model and/or the view to change as appropriate.

Source: Microsoft Docs

Additional resourses: (only useful ones)

External

  1. MVC Explanatory [computer science design patterns]
  2. Creating a Custom Controller and View in CodeIgniter [a visual example]
  3. Codeproject's definitions MVC: easy | extended

Internal

abberdeen
  • 323
  • 7
  • 32
1

The controllers are entry point of MVC, controllers call to model , and model check wich view display , example, magento (mvc)

Soleil
  • 381
  • 4
  • 9