Search code examples
phpjsonlithium

PHP lithium(li3) how to set up automatic response with JSON


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?


Solution

  • 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.