Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC: add http header: controller or view?

Where is a correct place to add http headers in MVC applications: in controller or in view?

(Technically it is possible to do either in controller or view, but it is not clear for me what solution better suites with MVC model)

like image 414
sergtk Avatar asked May 19 '11 16:05

sergtk


2 Answers

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

Therefore, since HTTP headers are a presentation detail, they should go in the view. A well-written MVC app would allow you to have non-web-based views (such as a desktop version) using the same controller and model. Putting the headers in the controller would break this clear separation.

like image 83
gdw2 Avatar answered Nov 05 '22 04:11

gdw2


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

A view may implement an HTTP header being sent to control such things as caching of content.

like image 36
AJ. Avatar answered Nov 05 '22 04:11

AJ.