• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP开源开发框架ZendFramework使用中常见问题说明及解决方案_php实例

php 搞代码 3年前 (2022-01-26) 44次浏览 已收录 0个评论

MVC 代码书写:
控制器代码书写:

<?php<BR>class IndexController extends Zend_Controller_Action<BR>{<BR>function init()<BR>{<BR>$this->registry = Zend_Registry::getInstance();<BR>$this->view = $this->registry['view'];<BR>$this->view->baseUrl = $this->_request->getBaseUrl();</P><P>}<BR>function indexAction()<BR>{<BR>$this->view->word=" I love spurs";</P><P>echo $this->view->render("index.html");</P><P>}<BR>function addAction(){<BR>//如果是POST过来的值.就增加.否则就显示增加页面</P><P><BR>}<BR>}<BR>?><BR>

控制当中写内容:

$this->view->word="ggg";<BR>$this->view->render("i<strong style="color:transparent">本文来源gaodai#ma#com搞@@代~&码*网/</strong><strong>搞gaodaima代码</strong>ndex.html");<BR>---->index.html echo $this->word;</P><P>application->config.ini <BR>[general]<BR>db.adapter=PDO_MYSQL<BR>db.config.host=localhost<BR>db.config.username=root<BR>db.config.password=<BR>db.config.dbname=think_zw<BR>

配置文件引入到framework里面去

//配置数据库参数,并连接数据库<BR>$config=new Zend_Config_Ini('./application/config/config.ini',null, true);<BR>Zend_Registry::set('config',$config);<BR>$dbAdapter=Zend_Db::factory($config->general->db->adapter,$config->general->db->config->toArray());<BR>$dbAdapter->query('SET NAMES UTF8');<BR>Zend_Db_Table::setDefaultAdapter($dbAdapter);<BR>Zend_Registry::set('dbAdapter',$dbAdapter);<BR>

单一入口模式:localhost/index/add/访问index模块下的add方法
function addAction(){}(在IndexController.php)
默认访问为index模块下的index方法

再建立一个模块model里面的message.php

<?php<BR>class Message extends Zend_Db_Table<BR>{<BR>protected $_name ="message";<BR>protected $_primary = 'id';<BR>}<BR>?> <BR>

模块实例化:

function indexAction()<BR>{<BR>$message=new message();//实例化数据库类</P><P>//获取数据库内容<BR>$this->view->messages=$message->fetchAll()->toArray();</P><P>echo $this->view->render('index.phtml');//显示模版<BR>}</P><P><?foreach($this->messages as $message): ?><BR><tr><BR><th><?php echo $message['title']; ?></th><BR><td><?php echo $message['content']; ?></td><BR></tr><BR><?endforeach; ?><BR>

*************
修改和删除数据

<?php if(2==2):?><BR>kk<BR><?php else:?><BR>ll<BR><?php endif;?><BR>

index.phtml里面加上

baseUrl?>/index/exit">编辑<BR>baseUrl?>/index/delete">删除<BR>

添加一个新的方法:edit.phtml

function editAction(){</P><P>$message = new Message();<BR>$db = $message->getAdapter();</P><P>if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){<BR>$id = $this->_request->getPost('id');<BR>$cid = $this->_request->getPost('cid');<BR>$title = $this->_request->getPost('title');</P><P>$set = array(<BR>'cid'=>$cid,<BR>'title'=>$title<BR>);<BR>$where = $db->quoteInto('id = ?',$id);<BR>//更新数据<BR>$message->update($set,$where);<BR>unset($set);<BR>echo '修改数据成功!view->baseUrl.'/index/index/">返回';<BR>}else{<BR>$id = $this->_request->getParam('id');<BR>$this->view->messages = $message->fetchAll('id='.$id)->toArray();<BR>echo $this->view->render('edit.phtml');<BR>}<BR>}</P><P><BR>function delAction(){<BR>$message = new Message();<BR>$id = (int)$this->_request->getParam('id');</P><P>if($id > 0){<BR>$where = 'id = ' . $id;<BR>$message->delete($where);<BR>}<BR>echo '删除数据成功!view->baseUrl.'/index/index/">返回';<BR>}<BR>

异常出现:

Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (index.php)' in<BR>

解决办法:在index.php中的

$frontController =Zend_Controller_Front::getInstance();后加上<BR>$frontController->setParam('useDefaultControllerAlways', true);<BR>

*******
id/3 等于以前的?id=3


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP开源开发框架ZendFramework使用中常见问题说明及解决方案_php实例

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址