下面建立数据驱动的MVC应用。基本页面如ZendFrameWork1.11.10(MVC部分)学习笔记(1)建立。
首先对页面进行优化,建立css控制页面。
修改ZendApp\application\layouts\layout.phtml
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "">
- <html xmlns="" xml:lang="en" lang="en">
- <head>
- <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
- <title>
- <?php echo $this->escape($this->title); ?>
- </title>
- <link rel="stylesheet" type="text/css" media="screen" href="baseUrl();?>/public/css/site.css"/>
- </head>
- <body>
- <div id="content">
-
- <?php echo $this->layout()->content; ?>
- <div></body>
- </html>
由于路径问题,需要取得基本路径:
ZendApp\application\views\helpers\BaseUrl.php
- <?php
- class Zend_View_Helper_BaseUrl
- {
- function baseUrl()
- {
- $fc = Zend_Controller_Front::getInstance();
- return $fc->getBaseUrl();
- }
- }
创建css文件。
ZendApp\public\css\site.css
- body,html{
- margin:0.5px;
- font-family: Verdana,sans-serif;
- }
- h1{
- font-size:1.4em;
- color: #008000;
- }
- a {
- color:#008000;
- }
- /* Table */
- th {
- text-align:left;
- }
- td,th{
- padding-right:5px;
- }
- /* style form */
- form dt {
- width:100px;
- display: block;
- float: left;
- clear: left;
- }
- form dd{
- margin-left:0;
- float: left;
- }
- form #submitbutton{
- margin-left: 100px;
- }
在mysql的数据库中创建表并创建测试数据:
- CREATE TABLE albums (
- id int(11) NOT NULL auto_increment,
- artist varchar(100) NOT NULL,
- title varchar(100) NOT NULL,
- PRIMARY KEY (id)
- );
- INSERT INTO albums (artist, title)
- VALUES
- ('Duffy', 'Rockferry'),
- ('Van Morrison', 'Keep It Simple');
配置数据库链接
ZendApp\application\config.ini
- [general]
- db.adapter = PDO_MYSQL
- db.params.host = localhost
- db.params.username = root
- db.params.password = root
- db.params.dbname = wangcc
其中=右边的值修改为你自己的值。
修改index.php加载数据库:
ZendApp\index.php
- <?php
- error_reporting(E_ALL|E_STRICT);
- ini_set('display_errors', 1);
- date_default_timezone_set('Europe/London');
- // 目录设置和类装载
- set_include_path('.' . PATH_SEPARATOR . './library/'
- . PATH_SEPARATOR . './application/models'
- . PATH_SEPARATOR . get_include_path());
- // include "Zend/Loader.php";
- // Zend_Loader::registerAutoload();
- require_once 'Zend/Loader/Autoloader.php';
- Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);
- // Load configuration
- $config = new Zend_Config_Ini("./application/config.ini","general");
- $registry = Zend_Registry::getInstance();
- $registry->set('config',$config);
- // Setup Database
- $db = Zend_Db::factory($config->db);
- Zend_Db_Table::setDefaultAdapter($db);
- // 设置控制器
- $frontController = Zend_Controller_Front::getInstance();
- $frontController->setParam('useDefaultControllerAlways', true);
- //$frontController->setBaseUrl('/ZendApp');
- $frontController->throwExceptions(true);
- $frontController->setControllerDirectory('./application/controllers');
- Zend_Layout::startMvc(array('layoutPath'=>'./application/layouts'));
- // run!
- $frontController->dispatch();
创建模型和form
ZendApp\application\models\Albums.php
- <?php
- class Albums extends Zend_Db_Table
- {
- protected $_name = 'albums';
- }
- ?>
ZendApp\application\models\AlbumForm.php
- <?php
- class AlbumForm extends Zend_Form
- {
- public function __construct($options = null)
- {
- parent::__construct($options);
- $this->setName('album');
- $id = new Zend_Form_Element_Hidden('id');
- $artist = new Zend_Form_Element_Text('artist');
- $artist->setLabel('Artist')
- ->setRequired(true)
- ->addFilter('StripTags')
- ->addFilter('StringTrim')
- ->addValidator('NotEmpty');
- $title = new Zend_Form_Element_Text('title');
- $title->setLabel('Title')
- ->setRequired(true)
- ->addFilter('StripTags')
- ->addFilter('StringTrim')
- ->addValidator('NotEmpty');
- $submit = new Zend_Form_Element_Submit('submit');
- $submit->setAttrib('id', 'submitbutton');
- $this->addElements(array($id, $artist, $title, $submit));
- }
- }
- ?>
修改控制器和views
ZendApp\application\controllers\IndexController.php
- <?php
- class IndexController extends Zend_Controller_Action
- {
- function indexAction()
- {
- $this->view->title="My Albums";
- $albums = new Albums();
- $this->view->albums = $albums->fetchAll();
- }
- function addAction()
- {
- $this->view->title="Add New Albums";
- $form = new AlbumForm();
- $form->submit->setLabel('Add');
- $this->view->form = $form;
- if ($this->_request->isPost()) {
- $formData = $this->_request->getPost();
- if ($form->isValid($formData)) {
- $albums = new Albums();
- $row = $albums->createRow();
- $row->artist = $form->getValue('artist');
- $row->title = $form->getValue('title');
- $row->save();
- $this->_redirect('/');
- } else {
- $form->populate($formData);
- }
- }
- }
- function editAction()
- {
- $this->view->title="Edit Albums";
- $form = new AlbumForm();
- $form->submit->setLabel('Save');
- $this->view->form = $form;
- if ($this->_request->isPost()) {
- $formData = $this->_request->getPost();
- if ($form->isValid($formData)) {
- $albums = new Albums();
- $id = (int)$form->getValue('id');
- $row = $albums->fetchRow('id='.$id);
- $row->artist = $form->getValue('artist');
- $row->title = $form->getValue('title');
- $row->save();
- $this->_redirect('/');
- } else {
- $form->populate($formData);
- }
- } else {
- // album id is expected in $params['id']
- $id = (int)$this->_request->getParam('id', 0);
- if ($id > 0) {
- $albums = new Albums();
- $album = $albums->fetchRow('id='.$id);
- $form->populate($album->toArray());
- }
- }
- }
- function deleteAction()
- {
- $this->view->title="Delete Albums";
- if ($this->_request->isPost()) {
- $id = (int)$this->_request->getPost('id');
- $del = $this->_request->getPost('del');
- if ($del == 'Yes' && $id > 0) {
- $albums = new Albums();
- $where = 'id = ' . $id;
- $albums->delete($where);
- }
- $this->_redirect('/');
- } else {
- $id = (int)$this->_request->getParam('id');
- if ($id > 0) {
- $albums = new Albums();
- $this->view->album = $albums->fetchRow('id='.$id);
- }
- }
- }
- }
ZendApp\application\views\scripts\index\add.phtml
- <html>
- <head>
- <title>
- <?php echo $this->escape($this->title);?>
- </title>
- </head>
- <body>
- <h1>
- <?php echo $this->escape($this->title);?>
- </h1>
- <?php echo $this->form;?>
- </body>
- </html>
ZendApp\application\views\scripts\index\delete.phtml
- <html>
- <head>
- <title>
- <?php echo $this->escape($this->title);?>
- </title>
- </head>
- <body>
- <h1>
- <?php echo $this->escape($this->title);?>
- </h1>
- <?php if ($this->album) :?>
- <p>Are you sure that you want to delete
- '
- escape($this->album->title); ?>' by
- '
- escape($this->album->artist); ?>'?
- </p>
- <form action="url(array('action'=>'delete')); ?>"
- method="post">
- <div>
- <input type="hidden" name="id" value="album->id; ?>" />
- <input type="submit" name="del" value="Yes" />
- <input type="submit" name="del" value="No" /></div>
- </form>
- <?php else: ?>
- <p>Cannot find album. </p>
- <?php endif;?>
- </body>
- </html>
ZendApp\application\views\scripts\index\edit.phtml
- <html>
- <head>
- <title>
- <?php echo $this->escape($this->title);?>
- </title>
- </head>
- <body>
- <h1>
- <?php echo $this->escape($this->title);?>
- </h1>
- <?php echo $this->form ;?>
- </body>
- </html>
ZendApp\application\views\scripts\index\index.phtml
- <html>
- <head>
- <title>
- <?php echo $this->escape($this->title);?>
- </title>
- </head>
- <body>
- <h1>
- <?php echo $this->escape($this->title);?>
- </h1>
- <p>
- <a href="url(array('controller'=>'index', 'action'=>'add'));?>">Add new album </a>
- </p>
- <table>
- <tr>
- <th>Title </th>
- <th>Artist </th>
- <th> </th>
- </tr>
- <?php foreach($this->albums as $album) : ?>
- <tr>
- <td>
- <?php echo $this->escape($album->title);?>
- </td>
- <td>
- <?php echo $this->escape($album->artist);?>
- </td>
- <td>
- <a href="url(array('controller'=>'index', 'action'=>'edit', 'id'=>$album->id));?>">Edit </a>
- <a href="url(array('controller'=>'index', 'action'=>'delete', 'id'=>$album->id));?>">Delete </a>
- </td>
- </tr>
- <?php endforeach; ?>
- </table>
- </body>
- </html>
这样我们就建立了一个数据驱动的ZendFramework的MVC应用。
阅读(2712) | 评论(0) | 转发(0) |