ZendFrameWork1.11.10(MVC部分)学习笔记(2)

简介: 下面建立数据驱动的MVC应用。基本页面如ZendFrameWork1.11.10(MVC部分)学习笔记(1)建立。 首先对页面进行优化,建立css控制页面。 修改ZendApp\application\layouts\layout.

下面建立数据驱动的MVC应用。基本页面如ZendFrameWork1.11.10(MVC部分)学习笔记(1)建立。

首先对页面进行优化,建立css控制页面。

修改ZendApp\application\layouts\layout.phtml

 

  1. !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4.     head>
  5.         meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
  6.         title>
  7.             ?php echo $this->escape($this->title); ?>
  8.         /title>
  9.         link rel="stylesheet" type="text/css" media="screen" href="<?php echo $this->baseUrl();?>/public/css/site.css"/>
  10.     /head>
  11.     body>
  12.         div id="content">
  13.             
  14.             ?php echo $this->layout()->content; ?>
  15.             div>/body>
  16.         /html>

由于路径问题,需要取得基本路径:

ZendApp\application\views\helpers\BaseUrl.php

 

  1. ?php
  2. class Zend_View_Helper_BaseUrl
  3. {
  4.     function baseUrl()
  5.     {
  6.         $fc = Zend_Controller_Front::getInstance();
  7.         return $fc->getBaseUrl();
  8.     }
  9. }
创建css文件。

ZendApp\public\css\site.css

  1. body,html{
  2.     margin:0.5px;
  3.     font-family: Verdana,sans-serif;
  4. }
  5. h1{
  6.     font-size:1.4em;
  7.     color: #008000;
  8. }
  9. a {
  10.     color:#008000;
  11. }
  12. /* Table */
  13. th {
  14.     text-align:left;
  15. }
  16. td,th{
  17.     padding-right:5px;
  18. }
  19. /* style form */
  20. form dt {
  21.     width:100px;
  22.     display: block;
  23.     float: left;
  24.     clear: left;
  25. }
  26. form dd{
  27.     margin-left:0;
  28.     float: left;
  29. }
  30. form #submitbutton{
  31.     margin-left: 100px;
  32. }

mysql的数据库中创建表并创建测试数据:

  1. CREATE TABLE albums (
  2. id int(11) NOT NULL auto_increment,
  3. artist varchar(100) NOT NULL,
  4. title varchar(100) NOT NULL,
  5. PRIMARY KEY (id)
  6. );
  7. INSERT INTO albums (artist, title)
  8. VALUES
  9. ('Duffy', 'Rockferry'),
  10. ('Van Morrison', 'Keep It Simple');

配置数据库链接

ZendApp\application\config.ini

  1. [general]
  2. db.adapter = PDO_MYSQL
  3. db.params.host = localhost
  4. db.params.username = root
  5. db.params.password = root
  6. db.params.dbname = wangcc

其中=右边的值修改为你自己的值。

修改index.php加载数据库:

ZendApp\index.php

  1. ?php
  2. error_reporting(E_ALL|E_STRICT);
  3. ini_set('display_errors', 1);
  4. date_default_timezone_set('Europe/London');
  5. // 目录设置和类装载

  6. set_include_path('.' . PATH_SEPARATOR . './library/'
  7. . PATH_SEPARATOR . './application/models'
  8. . PATH_SEPARATOR . get_include_path());
  9. // include "Zend/Loader.php";

  10. // Zend_Loader::registerAutoload();

  11. require_once 'Zend/Loader/Autoloader.php';
  12. Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);
  13. // Load configuration

  14. $config = new Zend_Config_Ini("./application/config.ini","general");
  15. $registry = Zend_Registry::getInstance();
  16. $registry->set('config',$config);
  17. // Setup Database

  18. $db = Zend_Db::factory($config->db);
  19. Zend_Db_Table::setDefaultAdapter($db);
  20. // 设置控制器

  21. $frontController = Zend_Controller_Front::getInstance();
  22. $frontController->setParam('useDefaultControllerAlways', true);
  23. //$frontController->setBaseUrl('/ZendApp');

  24. $frontController->throwExceptions(true);
  25. $frontController->setControllerDirectory('./application/controllers');
  26. Zend_Layout::startMvc(array('layoutPath'=>'./application/layouts'));
  27. // run!

  28. $frontController->dispatch();

创建模型和form

ZendApp\application\models\Albums.php

  1. ?php
  2. class Albums extends Zend_Db_Table
  3. {
  4.     protected $_name = 'albums';
  5. }
  6. ?>

ZendApp\application\models\AlbumForm.php

  1. ?php
  2. class AlbumForm extends Zend_Form
  3. {
  4.     public function __construct($options = null)
  5.     {
  6.         parent::__construct($options);
  7.         $this->setName('album');
  8.         $id = new Zend_Form_Element_Hidden('id');
  9.         $artist = new Zend_Form_Element_Text('artist');
  10.         $artist->setLabel('Artist')
  11.         ->setRequired(true)
  12.         ->addFilter('StripTags')
  13.         ->addFilter('StringTrim')
  14.         ->addValidator('NotEmpty');
  15.         $title = new Zend_Form_Element_Text('title');
  16.         $title->setLabel('Title')
  17.         ->setRequired(true)
  18.         ->addFilter('StripTags')
  19.         ->addFilter('StringTrim')
  20.         ->addValidator('NotEmpty');
  21.         $submit = new Zend_Form_Element_Submit('submit');
  22.         $submit->setAttrib('id', 'submitbutton');
  23.         $this->addElements(array($id, $artist, $title, $submit));
  24.     }
  25. }
  26. ?>

修改控制器和views

ZendApp\application\controllers\IndexController.php

  1. ?php
  2. class IndexController extends Zend_Controller_Action
  3. {
  4.     function indexAction()
  5.     {
  6.         $this->view->title="My Albums";
  7.         $albums = new Albums();
  8.         $this->view->albums = $albums->fetchAll();
  9.     }
  10.     function addAction()
  11.     {
  12.         $this->view->title="Add New Albums";
  13.         $form = new AlbumForm();
  14.         $form->submit->setLabel('Add');
  15.         $this->view->form = $form;
  16.         if ($this->_request->isPost()) {
  17.             $formData = $this->_request->getPost();
  18.             if ($form->isValid($formData)) {
  19.                 $albums = new Albums();
  20.                 $row = $albums->createRow();
  21.                 $row->artist = $form->getValue('artist');
  22.                 $row->title = $form->getValue('title');
  23.                 $row->save();
  24.                 $this->_redirect('/');
  25.             } else {
  26.                 $form->populate($formData);
  27.             }
  28.         }
  29.     }
  30.     function editAction()
  31.     {
  32.         $this->view->title="Edit Albums";
  33.         $form = new AlbumForm();
  34.         $form->submit->setLabel('Save');
  35.         $this->view->form = $form;
  36.         if ($this->_request->isPost()) {
  37.             $formData = $this->_request->getPost();
  38.             if ($form->isValid($formData)) {
  39.                 $albums = new Albums();
  40.                 $id = (int)$form->getValue('id');
  41.                 $row = $albums->fetchRow('id='.$id);
  42.                 $row->artist = $form->getValue('artist');
  43.                 $row->title = $form->getValue('title');
  44.                 $row->save();
  45.                 $this->_redirect('/');
  46.             } else {
  47.                 $form->populate($formData);
  48.             }
  49.         } else {
  50.             // album id is expected in $params['id']

  51.             $id = (int)$this->_request->getParam('id', 0);
  52.             if ($id > 0) {
  53.                 $albums = new Albums();
  54.                 $album = $albums->fetchRow('id='.$id);
  55.                 $form->populate($album->toArray());
  56.             }
  57.         }
  58.     }
  59.     function deleteAction()
  60.     {
  61.         $this->view->title="Delete Albums";
  62.         if ($this->_request->isPost()) {
  63.             $id = (int)$this->_request->getPost('id');
  64.             $del = $this->_request->getPost('del');
  65.             if ($del == 'Yes' && $id > 0) {
  66.                 $albums = new Albums();
  67.                 $where = 'id = ' . $id;
  68.                 $albums->delete($where);
  69.             }
  70.             $this->_redirect('/');
  71.         } else {
  72.             $id = (int)$this->_request->getParam('id');
  73.             if ($id > 0) {
  74.                 $albums = new Albums();
  75.                 $this->view->album = $albums->fetchRow('id='.$id);
  76.             }
  77.         }
  78.     }
  79. }

ZendApp\application\views\scripts\index\add.phtml

  1. html>
  2.     head>
  3.         title>
  4.             ?php echo $this->escape($this->title);?>
  5.         /title>
  6.     /head>
  7.     body>
  8.         h1>
  9.             ?php echo $this->escape($this->title);?>
  10.         /h1>
  11.         ?php echo $this->form;?>
  12.     /body>
  13. /html>

ZendApp\application\views\scripts\index\delete.phtml

  1. html>
  2.     head>
  3.         title>
  4.             ?php echo $this->escape($this->title);?>
  5.         /title>
  6.     /head>
  7.     body>
  8.         h1>
  9.             ?php echo $this->escape($this->title);?>
  10.         /h1>
  11.         ?php if ($this->album) :?>
  12.         p>Are you sure that you want to delete
  13. '
  14.             <?php echo $this->escape($this->album->title); ?>' by
  15. '
  16.             <?php echo $this->escape($this->album->artist); ?>'?
  17.         /p>
  18.         form action="<?php echo $this->url(array('action'=>'delete')); ?>"
  19. method="post">
  20.             div>
  21.                 input type="hidden" name="id" value="<?php echo $this->album->id; ?>" />
  22.                     input type="submit" name="del" value="Yes" />
  23.                     input type="submit" name="del" value="No" />/div>
  24.             /form>
  25.             ?php else: ?>
  26.             p>Cannot find album. /p>
  27.             ?php endif;?>
  28.         /body>
  29.     /html>

ZendApp\application\views\scripts\index\edit.phtml

  1. html>
  2.     head>
  3.         title>
  4.             ?php echo $this->escape($this->title);?>
  5.         /title>
  6.     /head>
  7.     body>
  8.         h1>
  9.             ?php echo $this->escape($this->title);?>
  10.         /h1>
  11.         ?php echo $this->form ;?>
  12.     /body>
  13. /html>

ZendApp\application\views\scripts\index\index.phtml

  1. html>
  2.     head>
  3.         title>
  4.             ?php echo $this->escape($this->title);?>
  5.         /title>
  6.     /head>
  7.     body>
  8.         h1>
  9.             ?php echo $this->escape($this->title);?>
  10.         /h1>

  11.         p>
  12.             a href="<?php echo $this->url(array('controller'=>'index', 'action'=>'add'));?>">Add new album /a>
  13.         /p>
  14.         table>
  15.             tr>
  16.                 th>Title /th>
  17.                 th>Artist /th>
  18.                 th>&nbsp; /th>
  19.             /tr>
  20.             ?php foreach($this->albums as $album) : ?>
  21.             tr>
  22.                 td>
  23.                     ?php echo $this->escape($album->title);?>
  24.                 /td>
  25.                 td>
  26.                     ?php echo $this->escape($album->artist);?>
  27.                 /td>
  28.                 td>
  29.                     a href="<?php echo $this->url(array('controller'=>'index', 'action'=>'edit', 'id'=>$album->id));?>">Edit /a>
  30.                     a href="<?php echo $this->url(array('controller'=>'index', 'action'=>'delete', 'id'=>$album->id));?>">Delete /a>
  31.                 /td>
  32.             /tr>
  33.             ?php endforeach; ?>
  34.         /table>
  35.     /body>
  36. /html>

这样我们就建立了一个数据驱动的ZendFrameworkMVC应用。

目录
相关文章
|
4月前
|
前端开发 Java 数据库连接
【1024程序员节】RestFul,mvc拦截器,SSM基本整合-学习笔记
【1024程序员节】RestFul,mvc拦截器,SSM基本整合-学习笔记
52 1
|
4月前
|
JSON 前端开发 Java
Spring mvc-文件上传与JSON-学习笔记
Spring mvc-文件上传与JSON-学习笔记
37 2
|
8月前
|
前端开发
前端学习笔记202305学习笔记第三十天-什么是mvc-m层的创建和数据展示
前端学习笔记202305学习笔记第三十天-什么是mvc-m层的创建和数据展示
34 0
|
8月前
|
前端开发 API
前端学习笔记202305学习笔记第三十天-什么是mvc-c层api 前后端联动1
前端学习笔记202305学习笔记第三十天-什么是mvc-c层api 前后端联动1
51 0
|
8月前
|
前端开发
前端学习笔记202305学习笔记第二十九天-什么是mvc-m层的创建和数据展示1
前端学习笔记202305学习笔记第二十九天-什么是mvc-m层的创建和数据展示1
23 0
|
8月前
|
前端开发 API
前端学习笔记202305学习笔记第三十一天-什么是mvc-c层api 前后端联动3
前端学习笔记202305学习笔记第三十一天-什么是mvc-c层api 前后端联动3
41 0
|
8月前
|
前端开发 API
前端学习笔记202305学习笔记第三十一天-什么是mvc-c层api 和mvc总结3
前端学习笔记202305学习笔记第三十一天-什么是mvc-c层api 和mvc总结3
32 0
|
8月前
|
前端开发 API
前端学习笔记202305学习笔记第三十一天-什么是mvc-c层api 和mvc总结4
前端学习笔记202305学习笔记第三十一天-什么是mvc-c层api 和mvc总结
43 0
|
8月前
|
前端开发 API
前端学习笔记202305学习笔记第三十天-什么是mvc-c层api 前后端联动3
前端学习笔记202305学习笔记第三十天-什么是mvc-c层api 前后端联动3
47 0
|
8月前
|
前端开发 API
前端学习笔记202305学习笔记第三十一天-什么是mvc-c层api 和mvc总结2
前端学习笔记202305学习笔记第三十一天-什么是mvc-c层api 和mvc总结2
40 0