MVC 代碼書寫:
控制器代碼書寫:
復制代碼 代碼如下:
<?php
class 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過來的值.就增加.否則就顯示增加頁面
}
}
?>
控制當中寫內容:
復制代碼 代碼如下:
$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裡面去
復制代碼 代碼如下:
//配置數據庫參數,並連接數據庫
$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)
默認訪問為index模塊下的index方法
再建立一個模塊model裡面的message.php
復制代碼 代碼如下:
<?php
class Message extends Zend_Db_Table
{
protected $_name ="message";
protected $_primary = 'id';
}
?>
模塊實例化:
復制代碼 代碼如下:
function indexAction()
{
$message=new message();//實例化數據庫類
//獲取數據庫內容
$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; ?>
*************
修改和刪除數據
復制代碼 代碼如下:
<?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>
添加一個新的方法:edit.phtml
復制代碼 代碼如下:
function 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);
//更新數據
$message->update($set,$where);
unset($set);
echo '修改數據成功!<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 '刪除數據成功!<a href="'.$this->view->baseUrl.'/index/index/">返回</a>';
}
異常出現:
復制代碼 代碼如下:
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