Alex Rivera | Logout

Cakephp - If Request is JSON?

Asked 2013-06-12T03:03:44.530
8

I have read the RequestHandler part in cookbook. There are isXml(), isRss(), etc. But there's no isJson().

Any other way to check whether a request is JSON?

So when the url is mysite.com/products/view/1.json it will give JSON data, but without .json it will give the HTML View.

Thanks

Edit
Report

1 Answer

11

I dont think cakePHP has some function like isJson() for json data, you could create your custom though, like:

//may be in your app controller
function isJson($data) {
  return (json_decode($data) != NULL) ? true : false; 
}
//and you can use it in your controller
if( $this->isJson($your_request_data) ) {
 ... 
}

Added: if you want to check .json extension and process accordingly, then you could do in your controller:

$this->request->params['ext']; //which would give you 'json' if you have .json extension
answered 2013-06-12T03:12:26.147

Your Answer