Search code examples
cakephpcontent-type

CakePHP RequestHandler: setContent/renderAs/respondAs .. what?


Can someone please explain these functions:

 RequestHandlerComponent::renderAs()
 RequestHandlerComponent::respondAs()
 RequestHandlerComponent::setContent()

It feels slightly redundant to have all three of them (as public methods anyway). If I want to respond to a request with a PDF file, does that mean I'd have to call all three functions? How should I use these in my controller?


Solution

  • They're all different. From the API Docs:

    renderAs
    Sets the layout and template paths for the content type defined by $type.

    I.e. more or less a shortcut for $this->layout = '...' and $this->render(...).

    respondAs
    Sets the response header based on type map index name. If DEBUG is greater than 2, the header is not set.

    Outputs header(...).

    setContent
    Adds/sets the Content-type(s) for the given name. This method allows content-types to be mapped to friendly aliases (or extensions), which allows RequestHandler to automatically respond to requests of that type in the startup method.

    Doesn't actually do anything to the output, just allows you to add new types that are not defined by default.

    For outputting a PDF (assuming you have it as a file already) you should actually use a Media View.