Zend Framework 1.6.0 RC 2初體驗

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. 建立基本目錄結構
[cc lang=”ini” tab_size=”2″ lines=”40″]kiang@kiang-ubuntu:~/public_html/zf$ mkdir application public
kiang@kiang-ubuntu:~/public_html/zf$ cd application/
kiang@kiang-ubuntu:~/public_html/zf/application$ mkdir controllers models views configs
kiang@kiang-ubuntu:~/public_html/zf/application$ cd ../public/
kiang@kiang-ubuntu:~/public_html/zf/public$ mkdir css images js[/cc]

4. 建立 /home/kiang/public_html/zf/.htaccess ,將所有請求引導到 public 目錄
[cc lang=”apache” tab_size=”2″ lines=”40″]RewriteEngine on
RewriteBase /~kiang/zf/
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L][/cc]

5. 建立 /home/kiang/public_html/zf/public/.htaccess
[cc lang=”apache” tab_size=”2″ lines=”40″]# 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[/cc]
*RewriteBase 是因為在使用者個人目錄下,會造成預設 Rewrite 出問題

5.5設定config.ini
[cc lang=”ini” tab_size=”2″ lines=”40″]; Production site configuration data
[production]
webhost = localhost
db.adapter = pdo_mysql
db.params.host = localhost
db.params.username = user
db.params.password = pass
db.params.dbname = dbname

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

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

6. 建立 /home/kiang/public_html/zf/public/index.php
[cc lang=”php” tab_size=”2″ lines=”40″]< ?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.'../application/configsy' . PATH_SEPARATOR . get_include_path()); include "Zend/Loader.php"; Zend_Loader::registerAutoload(); /* DB*/ // 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
$db = Zend_Db::factory($db_config->db->adapter, $db_config->db->params->toArray());
// Profiler
//$db->getProfiler()->setEnabled(true);
$registry->set(‘db’, $db);
Zend_Db_Table::setDefaultAdapter($db);
// Set DB Charset UTF8
$db->query(“SET NAMES ‘utf8′”);

/* cache */
$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);
$registry->set(‘cache’, $cache);

// 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();[/cc]

7. 建立 /home/kiang/public_html/zf/application/controllers/IndexController.php
[cc lang=”php” tab_size=”2″ lines=”40″]
< ?php class IndexController extends Zend_Controller_Action { function indexAction() { //cache if (Zend_Registry::isRegistered('cache')) { $this->cache = Zend_Registry::get(‘cache’);
$this->cache->save(‘title_val’,’title_key’);
$cache = $this->cache->load(‘cache_key’);
}
}
}[/cc]

8. 建立 /home/kiang/public_html/zf/application/views/layouts/layout.phtml
[cc lang=”php” tab_size=”2″ lines=”40″]< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

< ?php echo $this->escape($this->title); ?>

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

[/cc]

9. 建立 /home/kiang/public_html/zf/application/views/scripts/index/index.phtml
[cc lang=”php” tab_size=”2″ lines=”40″]

hello world

[/cc]

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

參考文件:
Zend Framework 1.6.0 RC 2 初體驗

發佈留言

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料