【问题标题】:I want to know how i can return Json in my cakephp when i perform a Get request?我想知道当我执行 Get 请求时如何在我的 cakephp 中返回 Json?
【发布时间】:2011-07-04 14:35:39
【问题描述】:

我是 cakephp 和 json 的新手。我最近用 cakephp 建立了一个休息服务。一切似乎都在工作。当我使用下面存储在单独文件中的此代码查询链接时:

<?php

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

我得到了 html 的返回值,因为我以这种方式编写了我的视图文件。她是我的视图文件: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>

我想知道如何在 JSON 输出中获取相同的数据。 这也是我的控制器文件:

 <?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));

    }


}

?>

【问题讨论】:

    标签: android web-services json cakephp


    【解决方案1】:

    假设你想保留你的 html 版本,并且还想提供一个 json 版本,那么你需要做几件事。

    1) 确保扩展被传递,以便您可以执行http://localhost/tut_blog/posts/index.jsonhttp://book.cakephp.org/view/1239/The-Simple-Setup 有信息(对于 *.xml,对于 *.json 的想法相同),但本质上您将以下内容添加到 app/config/routes.php

    Router::parseExtensions();
    

    2) 创建一个返回 json 的视图。对于您的情况,您可以在app/views/posts/json/index.ctp 看到类似于以下内容的视图...

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

    在上面的视图中,我为您留下了很多地方来插入调试内容,但是一旦您满意,您可以将其写得更紧凑 (&lt;?php echo $this-&gt;Js-&gt;object($posts); ?&gt;)。

    3) 确保您有一个 json 布局 (app/views/layouts/json/default.ctp):

    <?php
    echo $content_for_layout;
    ?>
    

    这是您可以做的最简单的布局,尽管对于我的一个客户,我们在其中加入了更多的智能,以便我们可以有一个返回错误的标准方法:

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

    这样,如果请求的url以.json结尾就会得到上面的视图,否则会使用默认的视图和布局(通常是html)。

    4) 您可能需要将 Js 助手添加到您的控制器中,或者更好的是 app_controller.php 为所有视图启用它。有可能你不需要添加它,因为 CakePHP 可能会根据布局自动包含它,但在添加 json 输出之前,我们在 app_controller.php 中有 Js 助手,所以我不能肯定地告诉你。

    【讨论】:

    • 哇,帮了大忙!感谢您的 101,我全面了解了如何解决我的问题。不幸的是,您给我的视图代码对我不起作用,我遇到了错误。但我改用了以下代码:&lt;?php echo json_encode($post); ?&gt; 再次感谢您分享您的知识。
    【解决方案2】:

    你可以在你的视图中使用Js helper的对象方法:

    echo $this->Js->object($posts);

    您可能还想使用 AJAX 布局来避免包含 default.ctp 布局中的所有 HTML:

    在您的控制器中:

    $this->layout = 'ajax';
    

    希望这会有所帮助!

    【讨论】:

    • 嗨,Alex,感谢您的意见。我用'echo json_encode($posts);'它以 json 格式从数据库中返回信息。 '{"Post":{"id":"2","title":"又是一个标题","body":"然后是帖子正文。","created":"2011-07-03 16 :14:33","修改":"0000-00-00 00:00:00"}}'。但我不明白你的代码是做什么的。这可能对我有帮助,您能否进一步解释一下?提前致谢!一月
    • 嗯,$this->Js->object() 是来自 JS 助手的方法,它将以 JSON 格式返回数据。我相信 json_encode() 是一个可用于 php 版本 > 5.2 的新函数。我可能会开始使用这个函数而不是 cakephp 的 JS 助手,因为它看起来会被弃用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 2021-11-28
    • 2019-06-11
    相关资源
    最近更新 更多