基于zf的留言本
附件: 您所在的用户组无法下载或查看附件
昵称: kinglion04 时间: 2007-08-22 12:48:00
看来越来越多的人喜欢zf了。哈。
昵称: xgwork 时间: 2007-08-22 15:55:00
好东西`~~.BANG
昵称: xing 时间: 2007-08-23 10:12:00
没有做到MC分离吧。。。:)
昵称: scauren 时间: 2007-08-23 11:24:00
今天我对程序做了修改,加入了smarty,也请提供意见。
昵称: kinglion04 时间: 2007-08-23 13:33:00
今天对zf留言本进行了修改。使用smarty模板进行显示。编程中需要注意几个地方:
1.zend framework默认是使用phtml模板进行显示的。比如indexAction对应的模板是views/scrīpts/index/index.phtml.使用别的模板前需要修改默认的viewrender.使用方法:$front->setParam('noViewRenderer', true); $front是一个Zend_Controller_Action实例。
2.定义Zend_View_Smarty类。这个类只是简单的实现Zend_View_Interface.代码可以直接从zend framework手册7.3中复制。只需在_construct中加上一行代码指定模板编译路径(compile_dir)。比如代码可以为$this->_smarty->compile_dir="./templates/templates_c";这个类定义我放在./library/Class目录下。在index.php中可以这么使用:$smarty = new Zend_View_Smarty();
$smarty->setscrīptPath("./templates/templates");
Zend_Registry::set('smarty',$smarty);
3.修改IndexController源码。主要还是在indexAction中改动。其实很简单。首先取出注册表中的视图View,然后从数据库中取出数据, 简单的assign方法就可以把数据对象messages传给模板。接着就用render方法呈现模板。看看源码就知道了。$view = Zend_Registry::get('smarty');
$db = Zend_Db_Table::getDefaultAdapter();
$sql = "SELECT * FROM `message` ORDER BY `id` DESC";
$result = $db->query($sql);
$Messages=$result->fetchAll();
$view->assign("messages",$Messages);
echo $view->render("index.tpl");
4.addAction只需加上几行代码:用于显示留言的窗体。这些内容不使用smarty时会由zend framework自动调用显示。使用smarty后就该显示的调用模板。if(){...} else
{
$view = Zend_Registry::get('smarty');
echo $view->render("add.tpl");
}
5.index.tpl怎么显示数据:使用section循环。可以查看smarty手册模仿使用。
<table class='entry' width="100%">
{section name=message loop=$messages}
<tr class='title'>
<td>{$messages[message].username}</td>
<td class='time'>{$messages[message].time}</td>
<td class= 'time'>{$messages[message].ip}</td>
</tr>
<tr class='content'>
<td>{$messages[message].content}</td>
</tr>
{/section}
</table>
1.zend framework默认是使用phtml模板进行显示的。比如indexAction对应的模板是views/scrīpts/index/index.phtml.使用别的模板前需要修改默认的viewrender.使用方法:$front->setParam('noViewRenderer', true); $front是一个Zend_Controller_Action实例。
2.定义Zend_View_Smarty类。这个类只是简单的实现Zend_View_Interface.代码可以直接从zend framework手册7.3中复制。只需在_construct中加上一行代码指定模板编译路径(compile_dir)。比如代码可以为$this->_smarty->compile_dir="./templates/templates_c";这个类定义我放在./library/Class目录下。在index.php中可以这么使用:$smarty = new Zend_View_Smarty();
$smarty->setscrīptPath("./templates/templates");
Zend_Registry::set('smarty',$smarty);
3.修改IndexController源码。主要还是在indexAction中改动。其实很简单。首先取出注册表中的视图View,然后从数据库中取出数据, 简单的assign方法就可以把数据对象messages传给模板。接着就用render方法呈现模板。看看源码就知道了。$view = Zend_Registry::get('smarty');
$db = Zend_Db_Table::getDefaultAdapter();
$sql = "SELECT * FROM `message` ORDER BY `id` DESC";
$result = $db->query($sql);
$Messages=$result->fetchAll();
$view->assign("messages",$Messages);
echo $view->render("index.tpl");
4.addAction只需加上几行代码:用于显示留言的窗体。这些内容不使用smarty时会由zend framework自动调用显示。使用smarty后就该显示的调用模板。if(){...} else
{
$view = Zend_Registry::get('smarty');
echo $view->render("add.tpl");
}
5.index.tpl怎么显示数据:使用section循环。可以查看smarty手册模仿使用。
<table class='entry' width="100%">
{section name=message loop=$messages}
<tr class='title'>
<td>{$messages[message].username}</td>
<td class='time'>{$messages[message].time}</td>
<td class= 'time'>{$messages[message].ip}</td>
</tr>
<tr class='content'>
<td>{$messages[message].content}</td>
</tr>
{/section}
</table>
昵称: kinglion04 时间: 2007-08-23 13:34:00
我下到本机,就链不上数据库了,
帮帮忙吧!~_~
帮帮忙吧!~_~
昵称: wwwwwwly 时间: 2007-11-16 16:11:00
提示异常是
Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (error)' in C:\AppServ\www\Lib\Zend\Controller\Dispatcher\Standard.php:194 Stack trace: #0 C:\AppServ\www\Lib\Zend\Controller\Front.php(920): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #1 C:\AppServ\www\www\index.php(23): Zend_Controller_Front->dispatch() #2 {main} thrown in C:\AppServ\www\Lib\Zend\Controller\Dispatcher\Standard.php on line 194
Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (error)' in C:\AppServ\www\Lib\Zend\Controller\Dispatcher\Standard.php:194 Stack trace: #0 C:\AppServ\www\Lib\Zend\Controller\Front.php(920): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #1 C:\AppServ\www\www\index.php(23): Zend_Controller_Front->dispatch() #2 {main} thrown in C:\AppServ\www\Lib\Zend\Controller\Dispatcher\Standard.php on line 194
昵称: wwwwwwly 时间: 2007-11-16 16:13:00
顶以下
昵称: wanghaip1982 时间: 2007-11-16 19:13:00
default Module 没有问题。换到 admin 主出现如下错误
ZF Version : 1.0.3[ 本帖最后由 dennis 于 2008-2-22 15:28 编辑 ]
ZF Version : 1.0.3
引用:
Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller class ("Admin_IndexController")' in C:\Program Files\Zend\Core For Oracle\ZendFramework\library\Zend\Controller\Dispatcher\Standard.php:300 Stack trace: #0 C:\Program Files\Zend\Core For Oracle\ZendFramework\library\Zend\Controller\Dispatcher\Standard.php(212): Zend_Controller_Dispatcher_Standard->loadClass('IndexController') #1 C:\Program Files\Zend\Core For Oracle\ZendFramework\library\Zend\Controller\Front.php(929): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #2 D:\ZFProjects\html\bootstrap.php(201): Zend_Controller_Front->dispatch() #3 D:\ZFProjects\html\index.php(40): Bootstrap::run() #4 {main} thrown in C:\Program Files\Zend\Core For Oracle\ZendFramework\library\Zend\Controller\Dispatcher\Standard.php on line 300
昵称: dennis 时间: 2008-02-22 15:27:00
引用:
Fatal error: Uncaught exception'Zend_Controller_Dispatcher_Exception' with message 'Invalid controllerclass ("Admin_IndexController")'

昵称: dennis 时间: 2008-02-22 17:22:00
写得很好,简单,适合入门
顶!!


顶!!
昵称: yaqy 时间: 2008-03-01 14:44:00
数据库的脚本语言没有提供吧??
昵称: silennina 时间: 2008-08-12 22:23:00
如题。
昵称: silennina 时间: 2008-08-12 22:46:00
Zend_View_Smarty这个类文件放在哪里呢?
随便放,了实例话时它可以自动找到嘛
随便放,了实例话时它可以自动找到嘛
昵称: dpf2cfd 时间: 2008-08-13 10:49:00
首先是数据对象即留言实例对应于数据库中的表:message
class Messages extends Zend_Db_Table
{
protected $_name="message";
}
直接派生于zendframework中的Zend_Db_Table.只需指定数据库中的表即可。访问muysql数据库只需指定默认适配器:
$config = new Zend_Config_Ini('./application/config.ini', 'config');
$db = Zend_Db::factory($config->db->adapter, $config->db->config->toArray());
Zend_Db_Table::setDefaultAdapter($db);
config.ini文件中包含有访问数据库需要的信息:使用本地服务器,用户名,密码,和数据库的名称。
接下去就是定义控制器Controller和行为Action了。
这里要处理的东西不多,主要有两件:一是呈现已有的留言,一个是添加留言。分别在连个函数中处理
indexAction
从数据库中取得数据,传递给模板:
function indexAction()
{
/* require_once('./application/models/Messages.php');
$messages=new Messages();
$message=$messages->fetchAll();//Zend_Db_Table_Rowset Object
$this->view->Messages=$message;
*/
$db = Zend_Db_Table::getDefaultAdapter();
$sql = "SELECT * FROM `message` ORDER BY `id` DESC";
$result = $db->query($sql);
$this->view->Messages=$result->fetchAll();//array object
}
我采用了两种方式,因为数据库返回对象不同。被我注释掉的那种方式返回的$message含有很多的无用数据。获取数据是也只能以属性的方式获取。即使用操作符->。
后一种方式返回的是数组。只包含要显示的留言信息。
查看数组形式的数据可以用print_r函数检验。
addAction
完成数据的检验和插入数据库操作。
if($this->_request->isPost())
{
$username = $this->_request->getPost('username');
$content = $this->_request->getPost('content');
if($username!=''&&$content!='')
{
require_once('./application/function.inc.php');
$time = time();
$ip = get_client_ip();
$data = array(
'username'=>$username,
'content' =>$content,
'time'=>$time,
'ip'=>$ip,
);
require_once('./application/models/Messages.php');
$messages=new Messages();
$messages->insert($data);
$this->_redirect('/');
}
}
首先检测是不是提交的数据,入如果不是则显示留言的界面,是则完成数据库insert并重定向到index页面显示提交的所有留言。
其它部分可以参看我的源码。我的改写没有使用smarty模板,主要是因为自己不熟悉smarty和zend framework之间相互操作。对内容的过滤方面也没有做,filter部分内容还差不多一窍不通。请大家提供意见。