{"id":1325,"date":"2017-11-10T14:58:16","date_gmt":"2017-11-10T13:58:16","guid":{"rendered":"http:\/\/www.netnea.com\/cms\/?p=1325"},"modified":"2017-11-10T14:58:16","modified_gmt":"2017-11-10T13:58:16","slug":"returning-json-from-a-view-in-cakephp-1-1","status":"publish","type":"post","link":"https:\/\/www.netnea.com\/cms\/2017\/11\/10\/returning-json-from-a-view-in-cakephp-1-1\/","title":{"rendered":"returning JSON from a view in CakePHP 1.1"},"content":{"rendered":"<p>I know CakePHP 3.5 is out, and before CakePHP 2.x and even before CakePHP 1.3 allowed to render JSON easily. But suppose you have a large code-base in CakePHP 1.1 you have to maintain, and suddenly someone wants JSON out of it&#8230;<\/p>\n<p>If upgrading your CakePHP version is possible, do it. Else, consider the code below.<\/p>\n<p>In your controller, find() your records :<\/p>\n<pre>function myctrfunc() {\r\n    $people = array('name' =&gt; 'Doe', 'given' =&gt; 'John');\r\n    Configure::write('debug', 0); \/\/ to avoid polluting the JSON\r\n    $this-&gt;set('people', $people);\r\n    $this-&gt;layout = \"json\";\r\n    $this-&gt;autoLayout = false;\r\n}\r\n<\/pre>\n<p>In your views\/yurctrl\/myctrfunc.thtml view, encode to JSON:<\/p>\n<pre>&lt;?php \r\nheader('Content-Type: application\/json'); \r\necho json_encode($people); \r\n?&gt;\r\n<\/pre>\n<p>create the layout you selected in the controler in app\/views\/layouts\/json.thtml:<\/p>\n<pre>&lt;?php \r\necho $content_for_layout; \r\n?&gt;\r\n<\/pre>\n<p>Success, you will get JSON in the browser:<\/p>\n<pre>{\r\n\"name\": \"Doe\",\r\n\"given\": \"John\"\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I know CakePHP 3.5 is out, and before CakePHP 2.x and even before CakePHP 1.3 allowed to render JSON easily. But suppose you have a large code-base in CakePHP 1.1 you have to maintain, and suddenly someone wants JSON out of it&#8230; If upgrading your CakePHP version is possible, do it. Else, consider the code [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":{"0":"post-1325","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-linux","7":"czr-hentry"},"_links":{"self":[{"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/posts\/1325","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/comments?post=1325"}],"version-history":[{"count":6,"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/posts\/1325\/revisions"}],"predecessor-version":[{"id":1331,"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/posts\/1325\/revisions\/1331"}],"wp:attachment":[{"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/media?parent=1325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/categories?post=1325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/tags?post=1325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}