Zend Framework 1.6.0 RC 2 初體驗

edited 十月 2013 in Zend Framework
1. 下載 Zend Framework 1.6.0 RC 2
http://framework.zend.com/download

2. 建立專案目錄 (/home/kiang/public_html/zf/) ,將下載檔案解壓縮後的 library 目錄複製到專案目錄 /home/kiang/public_html/zf/library
3. 建立基本目錄結構
[email protected]:~/public_html/zf$ mkdir application public
[email protected]:~/public_html/zf$ cd application/
[email protected]:~/public_html/zf/application$ mkdir controllers models views
[email protected]:~/public_html/zf/application$ cd ../public/
[email protected]:~/public_html/zf/public$ mkdir css images js
4. 建立 /home/kiang/public_html/zf/.htaccess ,將所有請求引導到 public 目錄
RewriteEngine on
RewriteBase    /~kiang/zf/
RewriteRule    ^$    public/    [L]
RewriteRule    (.*) public/$1    [L]
5. 建立 /home/kiang/public_html/zf/public/.htaccess
# Rewrite rules for Zend Framework
RewriteEngine on
RewriteBase    /~kiang/zf/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php
# Security: Don't allow browsing of directories
Options -Indexes
*RewriteBase 是因為在使用者個人目錄下,會造成預設 Rewrite 出問題

6. 建立 /home/kiang/public_html/zf/public/index.php
<?php
define('RootAppPath', '/home/kiang/public_html/zf/');
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', 1);
date_default_timezone_set('Asia/Taipei');
// directory setup and class loading
set_include_path('.' . PATH_SEPARATOR . RootAppPath . 'library/'
. PATH_SEPARATOR . RootAppPath . 'application/models'
. PATH_SEPARATOR . get_include_path());
include "Zend/Loader.php";
Zend_Loader::registerAutoload();
// setup controller
$frontController = Zend_Controller_Front::getInstance();
$frontController->throwExceptions(true);
$frontController->setControllerDirectory(RootAppPath . 'application/controllers');
$frontController->setBaseUrl('/~kiang/zf/');
Zend_Layout::startMvc(array('layoutPath'=> RootAppPath . 'application/views/layouts'));
// run!
$frontController->dispatch();

7. 建立 /home/kiang/public_html/zf/application/controllers/IndexController.php
<?php
class IndexController extends Zend_Controller_Action
{
    function indexAction() {
        $this->view->title = 'The first page!';
    }
}

8. 建立 /home/kiang/public_html/zf/application/views/layouts/layout.phtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">;
<html xmlns="http://www.w3.org/1999/xhtml"; 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>
</head>
<body>
<div id="content">
<h1><?php echo $this->escape($this->title); ?></h1>
<?php echo $this->layout()->content; ?>
</div>
</body>
</html>

9. 建立 /home/kiang/public_html/zf/application/views/scripts/index/index.phtml
<h1>hello world</h1>

10. 連到 http://localhost/~kiang/zf/ 看看是否正確 ;)

參考文件:
http://akrabat.com/zend-framework-tutorial/

原始討論: http://twpug.net/x/modules/newbb/viewtopic.php?topic_id=3574

評論

  • edited 八月 2008
    哈~kiang 把開頭都寫好了
    我接下來把連db和memcache 的部份寫上去

    1.
    在application 下建一個config 目錄
    然後建立一個config.ini 的檔案

    ; Production site configuration data
    [production]
    webhost = www.example.com
    db.adapter = pdo_mysql
    db.params.host = localhost
    db.params.username = username
    db.params.password = password
    db.params.dbname = dbname

    ; Staging site configuration data inherits from production and
    ; overrides values as necessary
    [staging : production]
    db.params.dbname = stagingdb

    [layout]
    layout = "default"
    layoutPath = "../app/views/layouts"

    [memcached]
    frontend.lifetime = 7200
    frontend.automatic_serialization = true
    backend.host = 192.168.1.1
    backend.port = 11211
    backend.persistent = false

    2.
    修改public 目錄下的index.php(bootstrap 檔案)

    //在autoload 之後load db

    /* DB */
    // include db config.ini
    $db_config = new Zend_Config_Ini('config.ini','staging'); //表示load staging 這個設定

    //存入registry
    $registry = Zend_Registry::getInstance();
    $registry->set('db_config', $db_config);

    // setup database
    $db = Zend_Db::factory($db_config->db->adapter, $db_config->db->params->toArray());
    $registry->set('db', $db);
    Zend_Db_Table::setDefaultAdapter($db);
    // Set DB Charset UTF8
    $db->query("SET NAMES 'utf8'");

    3.
    設定memcache

    /* cache */ //先load config
    $cache_config = new Zend_Config_Ini('config.ini','memcached');

    $frontendOptions = array(
    'lifetime' => $cache_config->frontend->lifetime, // cache lifetime of 2 hours
    'automatic_serialization' => $cache_config->frontend->automatic_serialization
    );

    // Directory where to put the cache files
    $server = array(
    'host' => $cache_config->backend->host,
    'port' => $cache_config->backend->port,
    'persistent' => $cache_config->backend->persistent
    );

    $backendOptions = array(
    'servers' => $server
    );


    // getting a Zend_Cache_Core object
    $cache = Zend_Cache::factory('Core', 'Memcached', $frontendOptions, $backendOptions);

    //一樣把cache register 起來
    $registry->set('cache', $cache);

    這樣index.php部份就差不多了


    4.
    建立models

    我們建先一個測試的table

    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');

    接著建立models/Albums.php

    <?php

    class Albums extends Zend_Db_Table
    {
    protected $_name = 'albums';//table 的名字
    function __construct()
    {
    if(Zend_Registry::isRegistered('db')) {
    $this->db = Zend_Registry::get('db');
    } else {
    // include db config.ini
    $db_config = new Zend_Config_Ini('config.ini','staging');
    $registry = Zend_Registry::getInstance();
    $registry->set('db_config', $db_config);

    // setup database
    $this->db = Zend_Db::factory($db_config->db->adapter, $db_config->db->params->toArray());
    $registry->set('db', $this->db);
    Zend_Db_Table::setDefaultAdapter($this->db);
    // Set DB Charset UTF8
    $this->db->query("SET NAMES 'utf8'");
    }

    //get cache
    if (Zend_Registry::isRegistered('cache')) {
    $this->cache = Zend_Registry::get('cache');
    }

    }
    }
    );


    再來去編輯
    application/Controller/IndexController.php

    function indexAction()
    {
    $this->view->title = 'The first page!';
    //建立models 物件
    $albums = new Albums();
    //取出albums 所有資料 assing 到view 裡的albums 這個變數
    $this->view->albums = $albums->fetchAll();

    }

    接著編輯
    application/views/scripts/index/index.phtml

    <?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="<?php echo $this->url(array('controller'=>'index',
    'action'=>'edit', 'id'=>$album->id));?>">Edit</a>
    <a href="<?php echo $this->url(array('controller'=>'index',
    'action'=>'delete', 'id'=>$album->id));?>">Delete</a>
    </td>
    </tr>
    <?php endforeach; ?>

    這樣就大功告成了...

    因為小第也還在摸索ZF
    若是有錯誤的地方,還請大家指教
  • edited 八月 2008
    RC3 剛剛推出了,希望不會繼續 RCn 了 ^^||
Sign In or Register to comment.