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)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With