view->render("index.html");..." />
 手机
当前位置:查字典教程网 >编程开发 >php教程 >PHP开源开发框架ZendFramework使用中常见问题说明及解决方案
PHP开源开发框架ZendFramework使用中常见问题说明及解决方案
摘要:MVC代码书写:控制器代码书写:复制代码代码如下:控制当中写内容:复制代码代码如下:$this->view->word="ggg";$thi...

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

【PHP开源开发框架ZendFramework使用中常见问题说明及解决方案】相关文章:

PHP模板引擎smarty详细介绍

PHP5对象体系

PHP截取中文字符串的问题

人大复印资料处理程序_查询篇

PHP开发文件系统实例讲解

PHP中路径问题的解决方案

用PHP连接Oracle数据库

PHP与已存在的Java应用程序集成

聊天室php&mysql(二)

php中文本数据翻页(留言本翻页)

精品推荐
分类导航