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