Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6116723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:14:21+00:00 2026-05-23T15:14:21+00:00

I’m new to cakephp and json. I’ve recently build a rest service with cakephp.

  • 0

I’m new to cakephp and json. I’ve recently build a rest service with cakephp. Everything seems to be working. When i query a link using this code beneath that is stored in a separate file:

<?php

$request =  'http://localhost/tut_blog/posts/'; 
$response = file_get_contents($request);
echo $response;
?>

I get the return in html because i programmed my view file that way. Her is my view file: index.ctp

<h2>View all posts</h2>

  <table>
    <tr>
    <th>Title</th>
    <th>Body</th>
     </tr>

<?php foreach($posts as $post):?> // met de functie foreach wordt er hier geloopt door de variable $posts die we eerder in de controller hadden aangemaakt en die alle waarden bezit van de query die we eerder hadden opgesteld.

<tr>
    <td><?php echo $post['Post']['title'];?></td> // de $posts array bevat 2 sleutels nr 1 = naam van de model, nr 2 = de naam van de veld in je tabel.
    <td><?php echo $post['Post']['body'];?></td>
</tr>

<?php endforeach; ?>
 </table>

I want to know how i can get this same data but then in JSON output.
Here’s my controller file as well:

 <?php 

class PostsController extends AppController{

var $name = 'Posts';

var $components = array('RequestHandler');

    function hello_world(){


    }


    function index(){


    $this->set('posts',$this->Post->find('all')); 

    }

    function view($id = NULL){ 


    $this->set('post',$this->Post->read(NULL, $id));

    }


}

?>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:14:22+00:00Added an answer on May 23, 2026 at 3:14 pm

    Assuming you want to keep your html version, and want to also supply a json version, then there are a couple of things you need to do.

    1) Make sure extensions are being passed so that you can do http://localhost/tut_blog/posts/index.json. There’s info at http://book.cakephp.org/view/1239/The-Simple-Setup (for *.xml, same idea for *.json), but in essence you add the following to app/config/routes.php:

    Router::parseExtensions();
    

    2) Create a view that returns json. For your case you could have a view at app/views/posts/json/index.ctp similar to the following…

    <?php
    $object = array();
    
    if (!empty($posts)) {
        $object = $posts;
    }
    
    $jsonObj = $this->Js->object($object);
    echo $jsonObj;
    ?>
    

    In the view above I’ve left plenty of places for you to insert debug stuff, but you could write it a lot more compact once you’re happy (<?php echo $this->Js->object($posts); ?>).

    3) Make sure you have a json layout (app/views/layouts/json/default.ctp):

    <?php
    echo $content_for_layout;
    ?>
    

    That’s the simplest layout you can do, although for one of my clients we’ve put a little more intelligence into it so that we can have a standard method of returning errors:

    <?php
    if (isset($errorMessage)) {
        $errorMessageData['error'] = $errorMessage;
        echo $this->Js->object($errorMessageData);
    } else {
        echo $content_for_layout;
    }
    ?>
    

    In this way, if the requested url ends with .json you’ll get the view above, otherwise the default view and layout will be used (usually html).

    4) You may need to add the Js helper to your controller, or better still app_controller.php to enable it for all views. There’s the possibility you won’t need to add it as CakePHP might auto include it based on the layout, but we’ve had the Js helper in app_controller.php since before adding json output, so I can’t tell you for sure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I want use html5's new tag to play a wav file (currently only supported
I'm looking for suggestions for debugging... If you view this site in Firefox or
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm making a simple page using Google Maps API 3. My first. One marker
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.