从cakephp发送xml响应

时间:2013-10-15 14:41:14

标签: php xml cakephp

我想从cakephp的控制器动作发送一个xml响应。我一直在尝试遵循官方指南:http://book.cakephp.org/2.0/en/views/json-and-xml-views.html

但我一直收到这个错误:

Missing View

Error: The view for ServicesController::index() was not found.

这就是我的服务控制器的样子:

<?php
class ServicesController extends AppController {

    var $uses = array();
    var $components = array('Auth','Session','RequestHandler');

    function beforeFilter() {
        $this->Auth->allow('index');
    }

    function index(){
        $output = array(
            "status" => "OK",
            "message" => "You are good"
        );

        $this->set('output', $output );
        $this->set('_serialize', array("output"));
   }
}
?>

我还在routes.php

添加了以下内容
Router::parseExtensions('json', 'xml');

可能是我做错了什么?

1 个答案:

答案 0 :(得分:-1)

您可以尝试将数组转换为XML,回显输出并退出函数:

function index(){
    $output = array(
        "root" => array( // It needs one top element (http://book.cakephp.org/2.0/en/core-utility-libraries/xml.html#transforming-an-array-into-a-string-of-xml)
            "status" => "OK",
            "message" => "You are good"
        )
    );

    $xmlObject = Xml::fromArray($output);
    echo $xmlObject->asXML();
    exit;
}