当前位置:首页 > CMS教程 > 其它CMS > 列表

PHP开源开发框架ZendFramework使用中常见问题说明及解决方案

发布:smiling 来源: PHP粉丝网  添加日期:2021-02-11 10:38:14 浏览: 评论:0 

Zend Framework(简写ZF)是由 Zend 公司支持开发的完全基于 PHP5 的开源PHP开发框架,可用于开发 Web 程序和服务,ZF采用 MVC(Model–View-Controller) 架构模式来分离应用程序中不同的部分方便程序的开发和维护。

MVC 代码书写:

控制器代码书写:

  1. <?php 
  2. class IndexController extends Zend_Controller_Action 
  3. { 
  4. function init() 
  5. { 
  6. $this->registry = Zend_Registry::getInstance(); 
  7. $this->view = $this->registry['view']; 
  8. $this->view->baseUrl = $this->_request->getBaseUrl(); 
  9. } 
  10. function indexAction() 
  11. { 
  12. $this->view->word=" I love spurs"; 
  13.  
  14. echo $this->view->render("index.html"); 
  15.  
  16. } 
  17. function addAction(){ 
  18. //如果是POST过来的值.就增加.否则就显示增加页面 
  19.  
  20.  
  21. } 
  22. } 
  23. ?> 

控制当中写内容:

  1. $this->view->word="ggg"; 
  2. $this->view->render("index.html"); 
  3. ---->index.html echo $this->word; 
  4. application->config.ini 
  5. [general] 
  6. db.adapter=PDO_MYSQL 
  7. db.config.host=localhost 
  8. db.config.username=root 
  9. db.config.password= 
  10. db.config.dbname=think_zw 

配置文件引入到framework里面去,代码如下:

  1. //配置数据库参数,并连接数据库 
  2. $config=new Zend_Config_Ini('./application/config/config.ini',null, true); 
  3. Zend_Registry::set('config',$config); 
  4. $dbAdapter=Zend_Db::factory($config->general->db->adapter,$config->general->db->config->toArray()); 
  5. $dbAdapter->query('SET NAMES UTF8'); 
  6. Zend_Db_Table::setDefaultAdapter($dbAdapter); 
  7. Zend_Registry::set('dbAdapter',$dbAdapter); 

单一入口模式:localhost/index/add/访问index模块下的add方法

function addAction(){}(在IndexController.php)

默认访问为index模块下的index方法

再建立一个模块model里面的message.php,代码如下:

  1. <?php 
  2. class Message extends Zend_Db_Table 
  3. { 
  4. protected $_name ="message"; 
  5. protected $_primary = 'id'; 
  6. } 
  7. ?> 

模块实例化:

  1. function indexAction() 
  2. { 
  3. $message=new message();//实例化数据库类 
  4. //获取数据库内容 
  5. $this->view->messages=$message->fetchAll()->toArray(); 
  6.  
  7. echo $this->view->render('index.phtml');//显示模版 
  8. } 
  9.  
  10. <?foreach($this->messages as $message): ?> 
  11. <tr> 
  12. <th><?php echo $message['title']; ?></th> 
  13. <td><?php echo $message['content']; ?></td> 
  14. </tr> 
  15. <?endforeach; ?> 
 

修改和删除数据,代码如下:

  1. <?php if(2==2):?> 
  2. kk 
  3. <?php else:?> 
  4. ll 
  5. <?php endif;?> 

index.phtml里面加上,代码如下:

  1. <a href="<?php echo $this->baseUrl?>/index/exit">编辑</a> 
  2. <a href="<?php echo $this->baseUrl?>/index/delete">删除</a> 

添加一个新的方法:edit.phtml,代码如下:

  1. function editAction(){ 
  2. $message = new Message(); 
  3. $db = $message->getAdapter(); 
  4.  
  5. if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){ 
  6. $id = $this->_request->getPost('id'); 
  7. $cid = $this->_request->getPost('cid'); 
  8. $title = $this->_request->getPost('title'); 
  9.  
  10. $set = array( 
  11. 'cid'=>$cid, 
  12. 'title'=>$title 
  13. ); 
  14. $where = $db->quoteInto('id = ?',$id); 
  15. //更新数据 
  16. $message->update($set,$where); 
  17. unset($set); 
  18. echo '修改数据成功!<a href="'.$this->view->baseUrl.'/index/index/">返回</a>'; 
  19. }else{ 
  20. $id = $this->_request->getParam('id'); 
  21. $this->view->messages = $message->fetchAll('id='.$id)->toArray(); 
  22. echo $this->view->render('edit.phtml'); 
  23. } 
  24. } 
  25.  
  26.  
  27. function delAction(){ 
  28. $message = new Message(); 
  29. $id = (int)$this->_request->getParam('id'); 
  30.  
  31. if($id > 0){ 
  32. $where = 'id = ' . $id; 
  33. $message->delete($where); 
  34. } 
  35. echo '删除数据成功!<a href="'.$this->view->baseUrl.'/index/index/">返回</a>'; 
  36. } 

异常出现:

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

Tags: ZendFramework

分享到: