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フレームワークには、さまざまなデータ形式の公開を解決するのに役立つアクションヘルパーコンテキストスイッチがあることを知っているかどうかわかりません

このコード スニペットは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 に答える