在 ServBay 建立與執行 Zend Framework (Laminas) 專案
概述
Zend Framework(現為 Laminas Project 一部分)是一套功能強大的開源 PHP 框架,提供許多高品質且物件導向的元件,適合用於建構現代 Web 應用程式與服務。它因具高度彈性、模組化設計與高效能著稱,是打造從簡易網站至企業級大型應用的理想選擇。
ServBay 是專為 macOS 與 Windows 而設計的本機網頁開發環境,內建 PHP、多種網頁伺服器(如 Caddy 和 Nginx)、資料庫(如 MySQL、PostgreSQL、MongoDB)、快取服務(如 Redis、Memcached)及其他開發工具。ServBay 讓您輕鬆設定與管理上述軟體,使本機架設與執行各種 PHP 框架專案變得非常簡單。
本文件將指引您如何在 ServBay 環境中建置並執行一個 Zend Framework (Laminas) 專案,並展示如何整合 ServBay 提供的資料庫與快取服務。
前置需求
在開始前,請確認您已完成以下準備:
- 安裝 ServBay: 已在 macOS 或 Windows 成功安裝並執行 ServBay。若尚未安裝,請造訪 ServBay 官方網站 取得下載與安裝指南。
- ServBay 軟體套件: 確認於 ServBay 安裝並啟動所需套件,包括:
- 至少一種 PHP 版本(建議使用 PHP 8.x 或更高,因 Zend Framework / Laminas 現代版本通常需求較新 PHP)。
- 網頁伺服器(Caddy 或 Nginx)。
- Composer(ServBay 一般預設隨附)。
- 您預計使用的資料庫效能(例如 MySQL、PostgreSQL)及快取服務(如 Memcached、Redis)。可於 ServBay 控制台輕鬆啟動這些服務。
建立 Zend Framework 專案
ServBay 建議將您的網站專案統一存放於以下路徑,以方便自動管理與設定:
- macOS:
/Applications/ServBay/www
- Windows:
C:\ServBay\www
進入網站根目錄
開啟終端機,切換至 ServBay 推薦的網站根目錄:
macOS:
bashcd /Applications/ServBay/www
1Windows:
cmdcd C:\ServBay\www
1使用 Composer 建立專案
ServBay 出廠即內建 Composer,無需另行安裝。使用 Composer 的
create-project
指令建立新的 Zend Framework (Laminas skeleton application) 專案。舉例將專案建於servbay-zend-app
子目錄下:bashcomposer create-project laminas/laminas-skeleton-application servbay-zend-app
1此步驟會將 Zend Framework (Laminas) 骨架應用下載至
servbay-zend-app
資料夾,並安裝所有所需套件。進入專案目錄
轉至新建的專案資料夾:
bashcd servbay-zend-app
1
設定網頁伺服器
為了能透過瀏覽器存取您的 Zend Framework 專案,需於 ServBay 設定網站。
- 開啟 ServBay 控制台: 啟動 ServBay 應用程式。
- 進入網站設定: 於 ServBay 控制台中,尋找並點選 網站 (Websites) 分頁。
- 新增網站: 點擊左下角「+」按鈕,新增網站設定。
- 填寫網站資訊:
- 名稱 (Name): 輸入容易識別的名稱,例如
My Zend Dev Site
。 - 網域 (Domain): 設定欲以瀏覽器存取的網域。建議使用
.local
或.test
結尾,例如servbay-zend-test.local
。ServBay 會自動配置本機 DNS。 - 網站型態 (Website Type): 選擇
PHP
。 - PHP 版本 (PHP Version): 指定此網站使用的 PHP 版本(例如
8.3
)。請確保該版本已於 ServBay 安裝並啟動。 - 網站根目錄 (Document Root): 即網站伺服器對外公開目錄。Zend Framework 的入口點
index.php
位於專案public
目錄,因此根目錄應指向/Applications/ServBay/www/servbay-zend-app/public
。
- 名稱 (Name): 輸入容易識別的名稱,例如
- 儲存並重啟: 點選 儲存 (Save)。ServBay 會提示套用變更,點選確認後,網頁伺服器將重載設定使新網站生效。
詳細設定步驟請參閱 ServBay 文件:新增第一個網站 章節。
基本 "Hello ServBay!" 範例
接下來,讓我們修改專案程式碼,使根 URL (/
) 輸出 "Hello ServBay!"。
設定路由與控制器 (module.config.php)
編輯專案根目錄下的
module/Application/config/module.config.php
檔案。確保內含以下基礎路由與控制器設定:php<?php declare(strict_types=1); namespace Application; use Laminas\Router\Http\Literal; use Laminas\Router\Http\Segment; use Laminas\ServiceManager\Factory\InvokableFactory; return [ 'router' => [ 'routes' => [ 'home' => [ 'type' => Literal::class, 'options' => [ 'route' => '/', 'defaults' => [ 'controller' => Controller\IndexController::class, 'action' => 'index', ], ], ], // ... 其他路由設定 ], ], 'controllers' => [ 'factories' => [ Controller\IndexController::class => InvokableFactory::class, ], ], 'view_manager' => [ 'display_not_found_reason' => true, 'display_exceptions' => true, 'doctype' => 'HTML5', 'not_found_template' => 'error/404', 'exception_template' => 'error/index', 'template_map' => [ 'layout/layout' => __DIR__ . '/../view/layout/layout.phtml', 'application/index/index' => __DIR__ . '/../view/application/index/index.phtml', 'error/404' => __DIR__ . '/../view/error/404.phtml', 'error/index' => __DIR__ . '/../view/error/index.phtml', ], 'template_path_stack' => [ __DIR__ . '/../view', ], ], // ... 其他設定 ];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49注意: 上述程式是
module.config.php
的部分內容,需合併進現有設定陣列中。務必確認'home'
路由和Controller\IndexController::class
工廠都存在。建立或修改控制器 (IndexController.php)
編輯或新建
module/Application/src/Controller/IndexController.php
。indexAction
方法需回傳一個帶有訊息的 ViewModel:php<?php declare(strict_types=1); namespace Application\Controller; use Laminas\Mvc\Controller\AbstractActionController; use Laminas\View\Model\ViewModel; class IndexController extends AbstractActionController { /** * 預設動作,顯示歡迎頁。 */ public function indexAction() { // 回傳 ViewModel,並將 'message' 變數傳到視圖 return new ViewModel([ 'message' => 'Hello ServBay!', ]); } // ... 其他 action 方法 }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24建立或修改視圖檔 (index.phtml)
編輯或建立
module/Application/view/application/index/index.phtml
。此檔將呈現控制器傳來的message
變數:php<h1><?php echo $this->message; ?></h1>
1這裡採用 Zend Framework (Laminas) 的視圖助手
$this->message
取得控制器資料。
訪問網站
開啟瀏覽器,造訪您於 ServBay 設定的網域,例如:https://servbay-zend-test.local
。
若一切設定正確,即可看到頁面顯示 Hello ServBay!
。代表您的 Zend Framework 專案已成功於 ServBay 執行。
資料庫與快取整合範例
ServBay 提供多種資料庫及快取服務。以下範例說明如何於 Zend Framework 專案中連接及使用 Memcached、Redis、MySQL 及 PostgreSQL。
重要提醒: 下列示範為獨立範例。實作上,您會依專案需求選用特定資料庫與快取,並以依賴注入方式管理連線。執行範例前,請確認 ServBay 已啟動相應服務(如 MySQL、PostgreSQL、Memcached、Redis)。
資料庫操作範例 — 建表
先介紹如何透過 Laminas DB 元件與資料庫互動,包括建立一個簡單的資料表。程式碼展示如何定義建表動作並手動執行,不使用完整 Laminas Migrations 工具。
安裝 Laminas DB 元件
專案根目錄下用 Composer 安裝 Laminas DB:
bashcomposer require laminas/laminas-db
1手動建資料庫
執行資料庫範例前,請於 ServBay 中的資料庫服務手動建立名為
servbay_zend_app
的資料庫。可透過 ServBay 控制台進入 phpMyAdmin、pgAdmin、MongoDB Compass 等管理工具。ServBay MySQL/MariaDB 預設使用者為root
,密碼為password
。PostgreSQL 預設使用者也是root
,密碼為password
。定義與執行建表腳本(範例)
建立一個 PHP 檔案(例如
create_users_table.php
,置於專案根目錄或暫存目錄),輸入以下程式碼建立users
表:php<?php // create_users_table.php use Laminas\Db\Adapter\Adapter; use Laminas\Db\Sql\Sql; // 假設使用 MySQL 或 MariaDB $adapter = new Adapter([ 'driver' => 'Pdo_Mysql', // 或 'Pdo_Pgsql' 'database' => 'servbay_zend_app', 'username' => 'root', 'password' => 'password', // ServBay 預設密碼 'hostname' => '127.0.0.1', // 'port' => 3306, // MySQL 預設 // 'port' => 5432, // PostgreSQL 預設 ]); $sql = new Sql($adapter); // 定義建立 users 表的 SQL $create = $sql->createTable('users') ->addColumn(new \Laminas\Db\Sql\Ddl\Column\Integer('id', false, null, ['AUTO_INCREMENT' => true])) ->addColumn(new \Laminas\Db\Sql\Ddl\Column\Varchar('name', 255)) ->addColumn(new \Laminas\Db\Sql\Ddl\Column\Varchar('email', 255, ['UNIQUE' => true])) ->addConstraint(new \Laminas\Db\Sql\Ddl\Constraint\PrimaryKey('id')); echo "執行 SQL:\n"; echo $sql->buildSqlString($create, $adapter->getPlatform()) . "\n"; try { // 執行 SQL $adapter->query( $sql->buildSqlString($create, $adapter->getPlatform()), Adapter::QUERY_MODE_EXECUTE ); echo "成功建立 'users' 資料表。\n"; } catch (\Exception $e) { echo "建表錯誤:" . $e->getMessage() . "\n"; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38注意: 此為手動執行範例。實務上,您會用 Laminas Migrations 管理資料表架構版本。
使用 PHP CLI 執行此腳本(確認檔案路徑):
bashphp create_users_table.php
1
MySQL 整合範例
說明如何於 Zend Framework 控制器連接並查詢 MySQL 資料庫。
設定資料庫連線
編輯
config/autoload/global.php
,輸入 MySQL 連線資訊。若已有db
設定請修改,否則新增:php<?php // config/autoload/global.php return [ 'db' => [ 'driver' => 'Pdo_Mysql', 'database' => 'servbay_zend_app', // 請確定已建立資料庫 'username' => 'root', // ServBay 預設 'password' => 'password', // ServBay 預設密碼 'hostname' => '127.0.0.1', 'port' => 3306, // MySQL 預設 'charset' => 'utf8mb4', ], // ... 其他全球設定 ];
1
2
3
4
5
6
7
8
9
10
11
12
13
14設定控制器工廠 (module.config.php)
為控制器注入
Laminas\Db\Adapter\Adapter
,需定義工廠。編輯module/Application/config/module.config.php
的controllers
部分,若已用InvokableFactory
請改為:php<?php // module/Application/config/module.config.php namespace Application; use Laminas\ServiceManager\Factory\InvokableFactory; use Laminas\Db\Adapter\AdapterInterface; return [ // ... 其他設定 'controllers' => [ 'factories' => [ Controller\IndexController::class => function($container) { $adapter = $container->get(AdapterInterface::class); return new Controller\IndexController($adapter); }, ], ], 'service_manager' => [ 'aliases' => [ AdapterInterface::class => 'Laminas\Db\Adapter\Adapter', ], 'factories' => [ 'Laminas\Db\Adapter\Adapter' => \Laminas\Db\Adapter\AdapterServiceFactory::class, ], ], // ... 其他設定 ];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27注意: 此為局部內容,需合併至既有設定,確保
AdapterInterface
別名及工廠都正確。設定路由 (module.config.php)
為 MySQL 範例加路由:
php<?php // module/Application/config/module.config.php namespace Application; use Laminas\Router\Http\Literal; return [ 'router' => [ 'routes' => [ // ... 既有路由(如 'home') 'mysql-add' => [ 'type' => Literal::class, 'options' => [ 'route' => '/mysql-add', 'defaults' => [ 'controller' => Controller\IndexController::class, 'action' => 'mysqlAdd', ], ], ], 'mysql' => [ 'type' => Literal::class, 'options' => [ 'route' => '/mysql', 'defaults' => [ 'controller' => Controller\IndexController::class, 'action' => 'mysql', ], ], ], ], ], // ... 其他設定 ];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34將這些路由加進
'routes'
子陣列。新增控制器方法 (IndexController.php)
修改
module/Application/src/Controller/IndexController.php
,加構造函式以接收注入的 Adapter,並加mysqlAddAction
與mysqlAction
:php<?php declare(strict_types=1); namespace Application\Controller; use Laminas\Mvc\Controller\AbstractActionController; use Laminas\View\Model\ViewModel; use Laminas\Db\Adapter\AdapterInterface; use Laminas\Db\Sql\Sql; class IndexController extends AbstractActionController { private $adapter; // 私有變數 public function __construct(AdapterInterface $adapter) { $this->adapter = $adapter; } /** * 預設動作,顯示歡迎頁。 */ public function indexAction() { return new ViewModel([ 'message' => 'Hello ServBay!', ]); } /** * 透過 MySQL 新增 user 到 'users' 資料表。 */ public function mysqlAddAction() { $sql = new Sql($this->adapter); $insert = $sql->insert('users') ->values([ 'name' => 'ServBay Demo User', 'email' => 'demo-mysql@servbay.test', ]); $statement = $sql->prepareStatementForSqlObject($insert); $result = $statement->execute(); $message = $result->getAffectedRows() > 0 ? 'MySQL 使用者新增成功。' : 'MySQL 使用者新增失敗。'; return new ViewModel([ 'message' => $message, ]); } /** * 透過 MySQL 取得所有 users。 */ public function mysqlAction() { $sql = new Sql($this->adapter); $select = $sql->select('users'); $statement = $sql->prepareStatementForSqlObject($select); $result = $statement->execute(); $users = []; foreach ($result as $row) { $users[] = $row; } // 將結果轉為 JSON 字串傳到視圖 return new ViewModel([ 'users' => json_encode($users, JSON_PRETTY_PRINT), ]); } // ... 其他 action 方法 }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76新增的構造函數與兩個 action 方法請加進類別。
建立視圖檔
新建
module/Application/view/application/index/mysql-add.phtml
:php<h1><?php echo $this->message; ?></h1>
1新建
module/Application/view/application/index/mysql.phtml
:php<h1>MySQL 使用者</h1> <pre><?php echo $this->users; ?></pre>
1
2訪問 MySQL 範例
確保 ServBay MySQL 啟動。 先訪問
https://servbay-zend-test.local/mysql-add
新增使用者。應能看到「MySQL 使用者新增成功。」的訊息。 再訪問https://servbay-zend-test.local/mysql
查看users
資料表資料(會以 JSON 顯示)。
PostgreSQL 整合範例
說明如何於控制器連接並查詢 PostgreSQL。
設定資料庫連線
編輯
config/autoload/global.php
設定 PostgreSQL 連線。注意: 若欲同時執行 MySQL/PGSQL 範例,可能需更複雜設定,此範例假設將'db'
設為 PostgreSQL:php<?php // config/autoload/global.php return [ 'db' => [ 'driver' => 'Pdo_Pgsql', 'database' => 'servbay_zend_app', 'username' => 'root', 'password' => 'password', 'hostname' => '127.0.0.1', 'port' => 5432, ], // ... 其他全球設定 ];
1
2
3
4
5
6
7
8
9
10
11
12
13設定控制器工廠 (module.config.php)
(同 MySQL 範例)請確認
module/Application/config/module.config.php
中工廠設定無誤。不需重複修改。設定路由 (module.config.php)
為 PostgreSQL 範例加路由:
php<?php // module/Application/config/module.config.php namespace Application; use Laminas\Router\Http\Literal; return [ 'router' => [ 'routes' => [ // ... 既有路由(如 'home', 'mysql-add', 'mysql') 'pgsql-add' => [ 'type' => Literal::class, 'options' => [ 'route' => '/pgsql-add', 'defaults' => [ 'controller' => Controller\IndexController::class, 'action' => 'pgsqlAdd', ], ], ], 'pgsql' => [ 'type' => Literal::class, 'options' => [ 'route' => '/pgsql', 'defaults' => [ 'controller' => Controller\IndexController::class, 'action' => 'pgsql', ], ], ], ], ], // ... 其他設定 ];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34加入
'routes'
子陣列。新增控制器方法 (IndexController.php)
修改
module/Application/src/Controller/IndexController.php
,新增pgsqlAddAction
及pgsqlAction
:php<?php declare(strict_types=1); namespace Application\Controller; use Laminas\Mvc\Controller\AbstractActionController; use Laminas\View\Model\ViewModel; use Laminas\Db\Adapter\AdapterInterface; use Laminas\Db\Sql\Sql; class IndexController extends AbstractActionController { private $adapter; public function __construct(AdapterInterface $adapter) { $this->adapter = $adapter; } // ... 既有 action(如 indexAction, mysqlAddAction, mysqlAction) /** * 透過 PostgreSQL 新增 user 到 'users'。 */ public function pgsqlAddAction() { $sql = new Sql($this->adapter); $insert = $sql->insert('users') ->values([ 'name' => 'ServBay Demo User', 'email' => 'demo-pgsql@servbay.test', ]); $statement = $sql->prepareStatementForSqlObject($insert); $result = $statement->execute(); $message = $result->getAffectedRows() > 0 ? 'PostgreSQL 使用者新增成功。' : 'PostgreSQL 使用者新增失敗。'; return new ViewModel([ 'message' => $message, ]); } /** * 透過 PostgreSQL 取得所有 users。 */ public function pgsqlAction() { $sql = new Sql($this->adapter); $select = $sql->select('users'); $statement = $sql->prepareStatementForSqlObject($select); $result = $statement->execute(); $users = []; foreach ($result as $row) { $users[] = $row; } return new ViewModel([ 'users' => json_encode($users, JSON_PRETTY_PRINT), ]); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65將兩方法加至類別。
建立視圖檔
新建
module/Application/view/application/index/pgsql-add.phtml
:php<h1><?php echo $this->message; ?></h1>
1新建
module/Application/view/application/index/pgsql.phtml
:php<h1>PostgreSQL 使用者</h1> <pre><?php echo $this->users; ?></pre>
1
2訪問 PostgreSQL 範例
確保 ServBay PostgreSQL 啟動。 先訪問
https://servbay-zend-test.local/pgsql-add
新增使用者。應能見到「PostgreSQL 使用者新增成功。」訊息。 再訪問https://servbay-zend-test.local/pgsql
查看users
資料表資料(以 JSON 呈現)。
Memcached 整合範例
示範如何於控制器使用 Memcached 快取。
安裝 Memcached 適配器
於專案根目錄用 Composer 安裝 Laminas Cache 的 Memcached 適配器:
json// composer.json { "require": { "laminas/laminas-skeleton-application": "^1.0", "laminas/laminas-cache-storage-adapter-memcached": "^2.0" // ... 其他相依 }, // ... 其他設定 }
1
2
3
4
5
6
7
8
9執行
composer update
安裝:bashcomposer update
1ServBay 已預載 PHP 的 memcached 擴充,無需另裝。
設定路由 (module.config.php)
為 Memcached 範例加路由:
php<?php // module/Application/config/module.config.php namespace Application; use Laminas\Router\Http\Literal; return [ 'router' => [ 'routes' => [ // ... 其他路由 'memcached' => [ 'type' => Literal::class, 'options' => [ 'route' => '/memcached', 'defaults' => [ 'controller' => Controller\IndexController::class, 'action' => 'memcached', ], ], ], ], ], // ... 其他設定 ];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24加到
'routes'
子陣列。新增控制器方法 (IndexController.php)
修改
module/Application/src/Controller/IndexController.php
,新增memcachedAction
:php<?php declare(strict_types=1); namespace Application\Controller; use Laminas\Mvc\Controller\AbstractActionController; use Laminas\View\Model\ViewModel; use Laminas\Cache\StorageFactory; use Laminas\Cache\Storage\StorageInterface; class IndexController extends AbstractActionController { // ... 構造函式與現有 action 方法 /** * 展示 Memcached 使用範例。 */ public function memcachedAction() { // 建立 Memcached 快取實例 // ServBay 的 Memcached 預設為 127.0.0.1:11211 $cache = StorageFactory::factory([ 'adapter' => [ 'name' => 'memcached', 'options' => [ 'servers' => [ ['127.0.0.1', 11211], ], 'ttl' => 300, // 快取有效期 300 秒 ], ], 'plugins' => [ 'serializer', 'exception_handler' => ['throw_exceptions' => false], ], ]); $cacheKey = 'my_memcached_data'; $cachedData = $cache->getItem($cacheKey, $success); if (!$success) { // 快取未命中 $cachedData = 'Hello Memcached!(原始資料已快取於 ' . date('Y-m-d H:i:s') . ')'; $cache->setItem($cacheKey, $cachedData); $cachedData .= ' - CACHE MISS'; } else { // 快取命中 $cachedData .= ' - CACHE HIT'; } return new ViewModel([ 'message' => $cachedData, ]); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56加進類別中。
建立視圖檔
新建
module/Application/view/application/index/memcached.phtml
:php<h1>Memcached 範例</h1> <p><?php echo $this->message; ?></p>
1
2訪問 Memcached 範例
確認 ServBay 開啟 Memcached。 訪問
https://servbay-zend-test.local/memcached
。首次會見到 "CACHE MISS" 訊息。有效期(此例 300 秒)內再次造訪,會見到 "CACHE HIT" 且時間不變,代表資料取自快取。
Redis 整合範例
說明如何於控制器使用 Redis 快取或儲存資料。
安裝 Redis 適配器
于專案目錄用 Composer 安裝 Laminas Cache 的 Redis 適配器:
json// composer.json { "require": { "laminas/laminas-skeleton-application": "^1.0", "laminas/laminas-cache-storage-adapter-redis": "^2.0", "ext-redis": "*" // ... 其他相依 }, // ... 其他設定 }
1
2
3
4
5
6
7
8
9
10執行
composer update
安裝:bashcomposer update
1ServBay 已預載 PHP 的 redis 擴充。
設定路由 (module.config.php)
為 Redis 範例加路由:
php<?php // module/Application/config/module.config.php namespace Application; use Laminas\Router\Http\Literal; return [ 'router' => [ 'routes' => [ // ... 其他路由 'redis' => [ 'type' => Literal::class, 'options' => [ 'route' => '/redis', 'defaults' => [ 'controller' => Controller\IndexController::class, 'action' => 'redis', ], ], ], ], ], // ... 其他設定 ];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24加進
'routes'
子陣列。新增控制器方法 (IndexController.php)
修改
module/Application/src/Controller/IndexController.php
,新增redisAction
:php<?php declare(strict_types=1); namespace Application\Controller; use Laminas\Mvc\Controller\AbstractActionController; use Laminas\View\Model\ViewModel; use Laminas\Cache\StorageFactory; use Laminas\Cache\Storage\StorageInterface; class IndexController extends AbstractActionController { // ... 構造函式與現有 action 方法 /** * 展示 Redis 使用範例。 */ public function redisAction() { // 建立 Redis 快取實例 // ServBay Redis 預設為 127.0.0.1:6379 $cache = StorageFactory::factory([ 'adapter' => [ 'name' => 'redis', 'options' => [ 'server' => [ 'host' => '127.0.0.1', 'port' => 6379, // 'database' => 0, // 'password' => null, ], 'ttl' => 300, ], ], 'plugins' => [ 'serializer', 'exception_handler' => ['throw_exceptions' => false], ], ]); $cacheKey = 'my_redis_data'; $cachedData = $cache->getItem($cacheKey, $success); if (!$success) { $cachedData = 'Hello Redis!(原始資料已快取於 ' . date('Y-m-d H:i:s') . ')'; $cache->setItem($cacheKey, $cachedData); $cachedData .= ' - CACHE MISS'; } else { $cachedData .= ' - CACHE HIT'; } return new ViewModel([ 'message' => $cachedData, ]); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57加進類別中。
建立視圖檔
新建
module/Application/view/application/index/redis.phtml
:php<h1>Redis 範例</h1> <p><?php echo $this->message; ?></p>
1
2訪問 Redis 範例
確認開啟 ServBay Redis。 訪問
https://servbay-zend-test.local/redis
。首次會見到 "CACHE MISS",有效期內重訪則見 "CACHE HIT"。
總結
經上述步驟,您已成功於 ServBay 本機開發環境中建立、設定並執行一個 Zend Framework (Laminas) 專案,學會如何利用 ServBay 網站功能設定網頁伺服器指向專案公開目錄,以及整合並使用 MySQL、PostgreSQL 資料庫與 Memcached、Redis 快取服務。
ServBay 讓本機開發環境的架設與管理變得更容易,幫助您專注撰寫程式或開發專案。善用 ServBay 豐富軟體包與彈性設定,您可輕鬆於本機模擬生產環境,提高開發效率。