zendFramework——zend命令建立項目(留言板)


㈠ 建議

   @(未配置ZendFramwork的同窗,在個人博客裏:[ NetBeans配置zendFramework ],觀看如何配置) php

   @  到網上下載個 zendFramework的API (建議下載英文文檔)。 css

            打開API,點擊 Learnint Zend Framework 路徑下的  html

                                - Zend Framework Quick Start  sql

   @ 根據裏面的教程走,就能建立一個‘留言板’。 數據庫


㈡ 用zend命令建立Zend Framwork項目的步驟:

    首先,咱們須要瞭解一下, 到底須要執行哪些步驟         bootstrap

  建立數據庫表
: test數據庫下,建立guestbook表
  配置 Bootstrap.php
:"引導文件"
  建立 layout
:"佈局"
④  配置 applicatin.ini
:"配置文件" :配置數據庫、佈局...信息
⑤  建立model :"模型",2部分:1實體bean; 2數據庫入口方法(select,insert,update,delete)
⑥  建立dbModel
:"表關聯模型":指定模型映射哪個數據表
  建立 mapper
:"模型入口實現" :模型的入口方法在這裏實現
  建立 Controller 
:"控制器":調用模型方法,獲值後傳遞給視圖
  建立 view
:"顯示層phtml",PHP的HTML頁面
⑩  演示


㈢ 注意事項:

建立 layout、Controller的時候,必定得用 zend命令 建立,不要手動建立。        app

■ zend命令建立:layout、controller的時候,會自動配置信息到application.ini 以及 .zfproject.xml。   
■ application.ini 是關於配置數據庫、佈局、視圖 ... 信息

■ .zfproject.xml文件在項目裏看不到,獲得你的工做空間所在的項目下才能看到。裏面是關於Controller調用Action的配置



@ 打開命令窗口                如圖:↓ ide

            



㈣ 建立zendFramework實例過程

建立數據庫表   ( 在test數據庫,建立 guestbook 表)            代碼以下↓

use test;

CREATE TABLE guestbook (
 id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
 email VARCHAR(32) NOT NULL DEFAULT 'noemail@test.com',
 comment TEXT NULL,
 created DATETIME NOT NULL
);

CREATE INDEX id ON guestbook(id);


INSERT INTO guestbook (email, comment, created) VALUES
    ('ralph.schindler@zend.com','Hello! Hope you enjoy this sample zf application!', now());

INSERT INTO guestbook (email, comment, created) VALUES
    ('foo@bar.com', 'Baz baz baz, baz baz Baz baz baz - baz baz baz.', now());


 修改Bootstrap.php  (引導文件) 佈局

   ./application/Bootstrap.php                             將如下代碼替換進去↓ 測試

/**
 * 添加引導資源的最簡單方法是隻需用短語_init建立一個受保護的方法開始。在這種狀況下,
 * 咱們想要初始化 doctype,因此咱們將咱們引導類別內建立_initDoctype()的一種方法:
 */
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initDoctype()
    {
        //初始化視圖
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->doctype('XHTML1_STRICT');
    }
}


 建layout (佈局)

    @ 執行zend命令,建立layout。          
       濾器: enable config

          

    

    @ 執行後,自動建立一個layout.ptml,地址以下:

    ./application/layouts/scripts/layout.ptml                       將如下代碼替換進去↓

<?php echo $this->doctype() ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Zend Framework Quickstart Application</title>
  <?php echo $this->headLink()->appendStylesheet('/css/global.css') ?>
</head>
<body>
<div id="header" style="background-color: #EEEEEE; height: 30px;">
    <div id="header-logo" style="float: left">
        <b>ZF Quickstart Application</b>
    </div>
    <div id="header-navigation" style="float: right">
        <a href="<?php echo $this->url(
            array('controller'=>'guestbook'),
            'default',
            true) ?>">Guestbook</a>
    </div>
</div>

<?php echo $this->layout()->content ?>

</body>
</html>


配置application.ini(配置數據庫)
 ./application/configs/application.ini                將如下代碼替換進去↓

[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0

;佈局     ( zend命令建立layout的時候,自動生成這一段信息 )
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"

;數據庫
resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = ogepyj
resources.db.params.dbname = test
resources.db.params.charset = utf8

;視圖
resources.view[] =

[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1

       

 建立模型Guestbook.php(模型:1 實體bean;2 增刪改查的入口方法)

     @ 在如下路徑建立一個Guestbook.php

     ./application/models/Guestbook.php                        將如下代碼替換進去↓

class Application_Model_Guestbook
{
    protected $_comment;
    protected $_created;
    protected $_email;
    protected $_id;
    protected $_mapper;

    public function __construct(array $options = null) {
        if (is_array($options)) {
            $this->setOptions($options);
        }
    }

    public function __set($name, $value){
        $method = 'set' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid guestbook property');
        }
        $this->$method($value);
    }

    public function __get($name){
        $method = 'get' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid guestbook property');
        }
        return $this->$method();
    }

    public function setOptions(array $options){
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }

    public function setComment($text){
        $this->_comment = (string) $text;
        return $this;
    }
    public function getComment(){
        return $this->_comment;
    }
    public function setEmail($email){
        $this->_email = (string) $email;
        return $this;
    }
    public function getEmail(){
        return $this->_email;
    }
    public function setCreated($ts){
        $this->_created = $ts;
        return $this;
    }
    public function getCreated(){
        return $this->_created;
    }
    public function setId($id){
        $this->_id = (int) $id;
        return $this;
    }
    public function getId(){
        return $this->_id;
    }
    public function setMapper($mapper){
        $this->_mapper = $mapper;
        return $this;
    }
    public function getMapper(){
        if (null === $this->_mapper) {
            $this->setMapper(new Application_Model_GuestbookMapper());
        }
        return $this->_mapper;
    }
    
    public function save(){
        $this->getMapper()->save($this);
    }
    public function find($id){
        $this->getMapper()->find($id, $this);
        return $this;
    }
    public function fetchAll(){
        return $this->getMapper()->fetchAll();
    }
}


建立表關聯模(指定模型映射哪個數據表)

     @ 在如下位置建立 Guestbook.php     (因爲DbTable文件夾不存在,因此得先建立)

    ./application/models/DbTable/Guestbook.php              將如下代碼替換進去↓

class Application_Model_DbTable_Guestbook extends Zend_Db_Table_Abstract
{
    /** Table name */
    protected $_name    = 'guestbook';
}

建立GuestbookMapper.php

   (模型入口實現:模型的入口方法(select、insert、update、delete),在這裏實現)  

  ./application/models/GuestbookMapper.php            將如下代碼替換進去↓

class Application_Model_GuestbookMapper
{
    protected $_dbTable;

    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable(){
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_Guestbook');
        }
        return $this->_dbTable;
    }

    public function save(Application_Model_Guestbook $guestbook){
        $data = array(
            'email'   => $guestbook->getEmail(),
            'comment' => $guestbook->getComment(),
            'created' => date('Y-m-d H:i:s'),
        );

        if (null === ($id = $guestbook->getId())) {
            unset($data['id']);
            $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array('id = ?' => $id));
        }
    }

    public function find($id, Application_Model_Guestbook $guestbook){
        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
            return;
        }
        $row = $result->current();
        $guestbook->setId($row->id)
                  ->setEmail($row->email)
                  ->setComment($row->comment)
                  ->setCreated($row->created);
    }

    public function fetchAll(){
        $resultSet = $this->getDbTable()->fetchAll();
        $entries   = array();
        foreach ($resultSet as $row) {
            $entry = new Application_Model_Guestbook();
            $entry->setId($row->id)
                  ->setEmail($row->email)
                  ->setComment($row->comment)
                  ->setCreated($row->created)
                  ->setMapper($this);
            $entries[] = $entry;
        }
        return $entries;
    }
}


 建立控制器GuestbookController.php(控制器:調用模型方法,獲值後傳遞給視圖)

    @ zend命令建立  GuestbookController.php     
        過濾器 :create controller   
          參數:Guestbook

      

     執行後,GuestbookController.php的路徑以下:

    ./application/controllers/GuestbookController.php                將如下代碼替換進去↓

class GuestbookController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $guestbook = new Application_Model_Guestbook();
        $this->view->entries = $guestbook->fetchAll();
    }
}


建立視圖index.phtml(視圖:顯示數據)

    @ 在如下位置建立一個 index.phtml  (因爲guestbook文件夾不存在,因此得先建立)

    ./application/views/scripts/guestbook/index.phtml                 將如下代碼替換進去↓

<!-- application/views/scripts/guestbook/index.phtml -->

<p><a href="<?php echo $this->url(
    array(
        'controller' => 'guestbook',
        'action'     => 'sign'
    ),
    'default',
    true) ?>">Sign Our Guestbook</a></p>

Guestbook Entries: <br />
<dl>
    <?php foreach ($this->entries as $entry): ?>
    <dt><?php echo $this->escape($entry->email) ?></dt>
    <dd><?php echo $this->escape($entry->comment) ?></dd>
    <?php endforeach ?>
</dl>


⑩ 演示   

    @輸入如下地址進行測試:(每一個人項目放的工做空間不同,以致於地址不同,測試的時候,要仔細對應)

         http://localhost/zendFramework/zendtest/public

    @ 在右側有個連接:Guestbook    點擊進去後,就是剛纔項目所作的效果    


@ 項目效果界面↓

    

相關文章
相關標籤/搜索