views:

965

answers:

1

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?

+5  A: 

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.

deceze
"For outputting a PDF (assuming you have it as a file already)" no, I'm generating one using FPDF. Thanks for clearing up `renderAs` and `respondAs` for me, but I'm confused about how/where I'd use `setContent`? Would it let you determine the response type by the URL or similar? eg: `mysite.com/reports/view/html` automatically responds with HTML, whereas `mysite.com/reports/view/pdf` responds with PDF?
nickf
Yes, almost. An "extension" should be parsed and acted upon, like `example.com/controller/action.xml`. AFAIU `setContent()` allows you to add custom extensions. Have a look at the startup method that the above documentation refers to: http://api.cakephp.org/view_source/request-handler-component/#l-171
deceze