浅谈Zend Framework结合Ajax Web开发

来源:互联网 发布:淘宝只有退货没有换货 编辑:程序博客网 时间:2024/05/16 05:49

http://www.zendchina.net/?action-viewnews-itemid-276

ZendChina资讯:很早就想些一些关于zf和ajax结合的文章了,恰好最近论坛上有人问,今天又有空,那就写了一下,希望能起到抛砖引玉的作用。ajax的基本使用和zf的配置我就不多说了,不熟悉的自己参考相应的文章。在这里我是使用prototype进行二次开发的。首先看一下/js/ajax_test.js文件的代码:(主要是处理ajax,有详细注释)


//测试ajax+zf
/**
作者:许立强<[email=feifengxlq@hotmail.com]feifengxlq@hotmail.com[/email]& gt;[url=http://www.phpobject.net/xlq/]http://www.phpobject.net/xlq/[/url]
时间:2006-9-28
注意:是在使用prototype的基础上封装的
*/
var global_targetid;
//当ajax在loading数据的时候,显示该界面
function ajax_load()
{
return 
"<div align='center'>正在载入,Loading.......</div>";
}
//获取指定URL地址的内容并显示到指定targetid的标签上显示
function ajax_get(url,targetid)
{
global_targetid=targetid
var myajax=new Ajax.Request(url,{method:'get',parameters:'',onComplete:show_ajax_get_rs});
document.getElementById(targetid).innerHTML=ajax_load();
}
function 
show_ajax_get_rs(myajax)
{
if(
myajax.readyState==&& myajax.status==200)
{
  
document.getElementById(global_targetid).innerHTML=myajax.responseText;
}
}


       然后是/app/viewer/test/ajax_test.php,是显示层。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[url=http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd]http://www.w3.org/TR/xhtml1/DTD/ ... l.dtd">[/url]
<
html xmlns="[url=http://www.w3.org/1999/xhtml]http://www.w3.org/1999/xhtml">[/url]
<
head>
<
meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<
title>无标题文档</title>
</
head>
<
script language="javascriptsrc="/zend/js/prototype.js"></script>
<script language="javascript" src="/zend/js/ajax_test.js"></script>
<body >
<h1>以下内容为AJAX测试显示内容
</h1><div id="ajax_test_show"></div>
<p><a href="javascript:ajax_get('/zend/index.php/test/page /','ajax_test_show')">上一页</a><a href="javascript:ajax_get(' /zend/index.php/test/nextpage/','ajax_test_show')">下一页</a>< /p>
</body>
</html>

       最后是数据控制层。/app/controller/testController.php文件的主要代码。(很简单,应该能看懂的^_^)
<?
class testController extends Zend_Controller_Action
{
private 
$viewer;

public function 
__construct(){
     
$this->viewer=Zend::registry('viewer');
}

public function 
indexAction(){
     echo 
$this->viewer->render('test/ajax_test.php');
}

public function 
pageAction(){
     
header("Cache-Control: no-cache, must-revalidate");
     
header('Content-type: text/html;charset=GB2312');
     for(
$i=0;$i<10;$i++)
       echo 
"这是第一个页面
"
;
}

public function 
nextpageAction(){
     
header("Cache-Control: no-cache, must-revalidate");
     
header('Content-type: text/html;charset=GB2312');
     for(
$i=0;$i<10;$i++)
       echo 
"这是第二个页面
"
;
}
}
?>

       这样就实现了一个基本的ajax功能,稍微修改下,就可以做成ajax分页的功能。下次我回结合我的分页类详细说说的。