MVC 代碼書寫:控制器代碼書寫:<?phpclass IndexController extends Zend_Controller_Action{function init() { $this->registry = Zend_Registry::getInstance(); $this->view = $this->registry['view']; $this->view->baseUrl = $this->_request->getBaseUrl();
}function indexAction() { $this->view->Word=" I love spurs";
echo $this->view->render("index.html");
} function addAction(){ //如果是POST過來的值.就增加.否則就顯示增加頁面 }}?>控制當(dāng)中寫內(nèi)容:$this->view->word="ggg"; $this->view->render("index.html");---->index.html echo $this->word;
application->config.ini [general] db.adapter=PDO_MySQL db.config.host=localhost db.config.username=root db.config.password= db.config.dbname=think_zw
配置文件引入到framework里面去//配置數(shù)據(jù)庫參數(shù),并連接數(shù)據(jù)庫$config=new Zend_Config_Ini('./application/config/config.ini',null, true);Zend_Registry::set('config',$config);$dbAdapter=Zend_Db::factory($config->general->db->adapter,$config->general->db->config->toArray());$dbAdapter->query('SET NAMES UTF8');Zend_Db_Table::setDefaultAdapter($dbAdapter);Zend_Registry::set('dbAdapter',$dbAdapter);
單一入口模式:localhost/index/add/訪問index模塊下的add方法 function addAction(){}(在IndexController.php) 默認(rèn)訪問為index模塊下的index方法
再建立一個(gè)模塊model里面的message.php<?phpclass Message extends Zend_Db_Table{PRotected $_name ="message";protected $_primary = 'id';}?> 模塊實(shí)例化:function indexAction() { $message=new message();//實(shí)例化數(shù)據(jù)庫類
//獲取數(shù)據(jù)庫內(nèi)容 $this->view->messages=$message->fetchAll()->toArray();
echo $this->view->render('index.phtml');//顯示模版 }
<?foreach($this->messages as $message): ?> <tr> <th><?php echo $message['title']; ?></th><td><?php echo $message['content']; ?></td> </tr> <?endforeach; ?>
*************修改和刪除數(shù)據(jù)
<?php if(2==2):?>kk <?php else:?>ll<?php endif;?>
index.phtml里面加上<a href="<?php echo $this->baseUrl?>/index/exit">編輯</a><a href="<?php echo $this->baseUrl?>/index/delete">刪除</a>
添加一個(gè)新的方法:edit.phtmlfunction editAction(){ $message = new Message(); $db = $message->getAdapter(); if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){ $id = $this->_request->getPost('id'); $cid = $this->_request->getPost('cid'); $title = $this->_request->getPost('title'); $set = array( 'cid'=>$cid, 'title'=>$title ); $where = $db->quoteInto('id = ?',$id); //更新數(shù)據(jù) $message->update($set,$where); unset($set); echo '修改數(shù)據(jù)成功!<a href="'.$this->view->baseUrl.'/index/index/">返回</a>'; }else{ $id = $this->_request->getParam('id'); $this->view->messages = $message->fetchAll('id='.$id)->toArray(); echo $this->view->render('edit.phtml'); } }
function delAction(){ $message = new Message(); $id = (int)$this->_request->getParam('id'); if($id > 0){ $where = 'id = ' . $id; $message->delete($where); } echo '刪除數(shù)據(jù)成功!<a href="'.$this->view->baseUrl.'/index/index/">返回</a>'; }
異常出現(xiàn):Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (index.php)' in
解決辦法:在index.php中的 $frontController =Zend_Controller_Front::getInstance();后加上$frontController->setParam('useDefaultControllerAlways', true);
*******id/3 等于以前的?id=3
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注