MVC: add http header: controller or view?

Where is the right place to add http headers in MVC applications: in the controller or in sight?

(Technically, this can be done either in the controller or in the view, but for me it is not clear which solution is best suited for the MVC model)

+7
source share
2 answers

The purpose of the MVC template is to give a clear separation of responsibilities. The view processes the presentation, the controller processes the events, and the model provides business logic. (I understand that many web frameworks don't exactly follow the MVC pattern. Django, for example, calls itself MVT (or something like that)).

Therefore, since HTTP headers are part of the presentation, they must be displayed in the view. A well-written MVC application will allow you to have non-web representations (like the desktop version) using the same controller and model. Putting headers in the controller will break this clear separation.

+3
source

I think it depends on the scenario. For example, the controller may require certain security credentials, and if they are not present in the request / session, the Location: header will be sent to the client, directing it to the login window.

A view may implement an HTTP header that is sent to manage things such as content caching.

+2
source

All Articles