PHP开源开发框架ZendFramework使用中常见问题说明及解决方案
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