0

我想在zend框架上创建并返回带有ajax的xml文件

通常知道如何在没有框架的情况下做到这一点:

1-> 使用 php 创建一个 xml php 文件并将我的 table_db 写入我的 xml 文件,如下所示:

 => file name for exemple myfile.php 
 $xml = new DomDocument("1.0", "utf-8");
 $root = $xml->createElement("interventions");
 ....//write my table from db on my xml file 
 $xml->FormatOutput=true;
 $xml_string = $xml->saveXML();
 $xml->save("myfile.xml");

2-> 创建 ajax 请求并像这样从 myfile.php 读取 xml

if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
data=xmlhttp.responseXML;
alert(data.getElementsByTagName("titre_annonce")[0].firstChild.nodeValue);
// call a function here()  
}

}
xmlhttp.open("GET","myfile.php",true);
xmlhttp.send();

但我不知道如何使用 zendframe 进行所有程序

谢谢你的帮助。

4

1 回答 1

1

不知道大家知不知道zend framework有一个action helper context switch来帮助解决暴露不同的数据格式

此代码片段收集自http://www.zfsnippets.com/snippets/view/id/48

## /application/controllers/EnglandController.php
<?php
/**
 * Simple example of using the context switch
 *
 * @author      Dave Marshall 
 */

class EnglandController extends Zend_Controller_Action
{
    public function init()
    {
        $this->_helper->contextSwitch()
                      ->addActionContext('a', array('xml', 'json'))
                      ->setAutoJsonSerialization(true)
                      ->initContext();
    }

    public function aAction()
    {
        $this->view->team = 'a';
        $this->view->players = array(
            'David James',
            'Ashley Cole',
            'John Terry',
            'Rio Ferdinand',
            'Glen Johnson',
            'Joe Cole',
            'Steven Gerrard',
            'Frank Lampard',
            'David Beckham',
            'Wayne Rooney',
            'Michael Owen',
        );
    }
}

## /application/views/scripts/england/a.xml.phtml
<?php echo "<?xml version="1.0" ?>";?>
<team>
<name><?php echo $this->team; ?></name>
<players>
    <?php foreach($this->players as $player):?>
        <player><?php echo $player; ?></player>
    <?php endforeach; ?>
</players>
</team>

## http://yourhost/england/a/format/xml would output
<?xml version="1.0" ?><team>
<name>a</name>
<players>
            <player>David James</player>
            <player>Ashley Cole</player>
            <player>John Terry</player>
            <player>Rio Ferdinand</player>
            <player>Glen Johnson</player>

            <player>Joe Cole</player>
            <player>Steven Gerrard</player>
            <player>Frank Lampard</player>
            <player>David Beckham</player>
            <player>Wayne Rooney</player>
            <player>Michael Owen</player>

    </players>
</team>

## http://yourhost/england/a/format/json would output
{
    "team":"a",
    "players":[
        "David James",
        "Ashley Cole",
        "John Terry",
        "Rio Ferdinand",
        "Glen Johnson",
        "Joe Cole",
        "Steven Gerrard",
        "Frank Lampard",
        "David Beckham",
        "Wayne Rooney", 
        "Michael Owen"
    ]
}
于 2013-09-23T08:56:49.487 回答