1

I'm having some problems with deciding where to set the content-type header in a vanilla mvc framework. Should i add it inside the controllers method:

class ApiController extends Controller{

    public static function v1(){
        header('Content-Type: application/json');
        //some logic to output some json
    }    

} 

My mvc works more like an mvp:

Model <--> Controller <--> View
3

As talking about content-type headers only makes sense in the context of a remotely rendered view, as it typical for web-applications, I will assume that is the case here as well.

The content-type header, as well as other headers, are part of the communication mechanism for transferring the View information from the back-end part to the front-end (view rendering) part of the application.

The communication mechanism is an application concern and not a business logic concern, because it would change when you use different technologies to build the application. This means that it definitely does not belong in the Model part.

If setting the header should be part of the Controller or the View/Presenter depends mostly on the MVC/MVP framework that you use. In most frameworks for web applications, all communication with the front-end is handled by the Controller part and the View part only does a transformation of the Model data into a format that the front-end or browser can understand. With these frameworks, setting the content-type header would be a responsibility of the Controller. Other frameworks could work differently and have more communication logic in the View part and then it might be more straightforward to set the header in the View.

5
  • So basically i should create those headers right before rendering the view?
    – emma
    Jul 22 '19 at 7:48
  • 1
    @emma: The actual rendering is done in the browser. The headers should be created right next to the code where you build the rest of the message to send over the wire. Jul 22 '19 at 7:53
  • right next or right before? O_o
    – emma
    Jul 22 '19 at 7:57
  • @emma, that doesn't matter, as long as it is before the sending call. Jul 22 '19 at 7:58
  • ok then i'll create them right before :P :D. Thank you! :D
    – emma
    Jul 22 '19 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.