mvc 代码书写:
控制器代码书写:
复制代码 代码如下:
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
复制代码 代码如下:
模块实例化:
复制代码 代码如下:
function indexaction()
{
$message=new message();//实例化数据库类//获取数据库内容
$this->view->messages=$message->fetchall()->toarray();
echo $this->view->render('index.phtml');//显示模版
}
messages as $message): ?>
*************
修改和删除数据
复制代码 代码如下:
kk
ll
index.phtml里面加上
复制代码 代码如下:
/index/exit>编辑
/index/delete>删除
添加一个新的方法: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 '修改数据成功!view->baseurl.'/index/index/>返回';
}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 '删除数据成功!view->baseurl.'/index/index/>返回';
}
异常出现:
复制代码 代码如下:
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
http://www.bkjia.com/phpjc/788645.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/788645.htmltecharticlemvc 代码书写: 控制器代码书写: 复制代码 代码如下: ?php class indexcontroller extends zend_controller_action { function init() { $this-registry = zend_registry...