2

I have a lithium app set up that way, so when

return($data) 

is used it either can be used in the lithium view.html.php as

echo $data

or if request header "accept" equals "json/javacript" it would return something like

{
data: { a:'b' }
}

automatically.

Unfortunately in the new app that I made as a test app that flow is not happening (and only HTML is always returned).

After doing a little research it seems like that it is supposed to be done automatically if I uncomment

require __DIR__ . '/bootstrap/media.php';

inside bootstrap.php But that didn't work, I still have HTML returned. I downloaded a recent version of the lithium framework(I downloaded it may be 1 or 2 months ago)

Anybody knows if automatic response with JSON requires some set up or not?

ton.yeung
  • 4,793
  • 6
  • 41
  • 72
Evgenius
  • 935
  • 1
  • 8
  • 17

2 Answers2

2

taken from http://dev.lithify.me/lithium/tickets/view/353

which is then taken from the lithium docs

To enable automatic content-type negotiation (i.e. determining the content type of the response based on the value of the HTTP Accept header), set the 'negotiate' flag to true. Otherwise, the response will only be based on the type parameter of the request object (defaulting to 'html' if no type is present in the Request parameters)

http://li3.me/docs/lithium/action/Controller::$_render

If you need more help on how to implement leave a comment.

botero
  • 598
  • 2
  • 11
  • 23
ton.yeung
  • 4,793
  • 6
  • 41
  • 72
  • The answer is correct thank you! Small addition: in order to make it working one must put $this->_render['negotiate'] = true; code inside the _init function of a controller, if put inside an action of controller it doesn't work. The full version of the code looks like protected function _init() { // the negotiate option tells li3 to serve up the proper content type $this->_render['negotiate'] = true; parent::_init(); } – Evgenius Mar 18 '12 at 07:39
  • its strange. i return JSON and i did net configure it in this way. but i dont know anything specific to set it up, i just tried it once and it worked. i enabled media.php – Tomen Mar 21 '12 at 14:49
  • @Tomen I think its the way the request for JSON is coming through. Explicit in the controller or from the content-type or in the URL. – ton.yeung Mar 21 '12 at 17:24
  • something i forgot when i posted this comment. i explicitely gave the media type in the url, something like host://controller/action.json – Tomen Mar 24 '12 at 08:56
2

It is also possible to set type to $this->request->accepts() when calling render().

return $this->render(array('type' => $this->request->accepts()));
Nils
  • 2,041
  • 1
  • 15
  • 20