3

In my CakePHP app I return JSON and exit for certain requests. An example of this would be trying to access the API for a login as a GET request:

header('Content-Type: application/json');
echo json_encode(array('message'=>'GET request not allowed!'));
exit;

However I am having to prefix the echo with the content type in order for it to be sent as JSON. Otherwise my code at the other end interprets it different.

Any ideas on how to get around this? Or at least improve it.

Update: Cake version 2.3.0

7
  • 3
    This is the correct header for JSON content. There's nothing wrong with prefixing the header to your content. This is how it should be.
    – Boaz
    Feb 18, 2013 at 21:51
  • 3
    Actually, PHP has spoiled you by automatically sending an HTML Content-Type if you don't do so explicitly. So this is indeed how it should be.
    – Jon
    Feb 18, 2013 at 21:54
  • 1
    If this bothers you, just create a wrapper function like send_my_json($array) which sends the correct header() then calls json_encode() on the supplied argument. Feb 18, 2013 at 21:58
  • I wonder why a) you did not mention your cake version (by now you should know that this is vital to get correct answers) and b) you don't use the response object for 2.x (as you probably already know of its existence..)
    – mark
    Feb 18, 2013 at 22:00
  • 1
    json's just plain text, so text/plain would be a valid mime type as well. what matters if how the receiving end goes. e.g. jquery can use the mime type to detect json and automatically decide it for you.
    – Marc B
    Feb 18, 2013 at 22:01

1 Answer 1

38

You can leverage the new 2.x response object:

public function youraction() {
    // no view to render
    $this->autoRender = false;
    $this->response->type('json');

    $json = json_encode(array('message'=>'GET request not allowed!'));
    $this->response->body($json);
}

See http://book.cakephp.org/2.0/en/controllers/request-response.html#cakeresponse

Also you could use the powerful rest features and RequestHandlerComponent to achieve this automatically as documented: http://book.cakephp.org/2.0/en/views/json-and-xml-views.html

You just need to allow the extension json and call your action as /controller/action.json. Then cake will automatically use the JsonView and you can just pass your array in. It will be made to JSON and a valid response by the view class.

Both ways are cleaner than your "exit" solution - try to unit-test code that contains die()/exit(). This will end miserably. So better never use it in your code in the first place.

6
  • Quick question on this subject then. I asked a question here: stackoverflow.com/questions/14945418/… where setting the header for the content type was breaking the Access Control set earlier in the beforeFiler/beforeRender. Could you shed light on this? As your solution answered here fixes the problem in that question too. Thanks.
    – Cameron
    Feb 18, 2013 at 23:33
  • yes, headers can be overwritten. that's why you never manually output headers when you can easily use the response object to do it the clean way.
    – mark
    Feb 18, 2013 at 23:40
  • @mark stackoverflow.com/questions/42810819/… please look at this issue.
    – Naresh
    Mar 15, 2017 at 16:07
  • @mark this solution not works for me.. please take a look at my issue. thanks
    – Naresh
    Mar 15, 2017 at 16:27
  • 1
    In 3.x the response is immutable - you need to say $this->response = $this->response->with...() etc.
    – mark
    Mar 14, 2018 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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