สร้างและใช้งานโปรเจกต์ Zend Framework (Laminas) บน ServBay
ภาพรวม
Zend Framework (ปัจจุบันชื่อ Laminas Project) เป็นเฟรมเวิร์ก PHP แบบโอเพ่นซอร์สที่ทรงพลัง มาพร้อมคอมโพเนนต์เชิงวัตถุคุณภาพสูงสำหรับสร้างเว็บแอปพลิเคชันและบริการที่ทันสมัย ด้วยจุดเด่นเรื่องความยืดหยุ่น การออกแบบแบบแยกโมดูล และประสิทธิภาพสูง Zend Framework เป็นตัวเลือกที่ยอดเยี่ยม ตั้งแต่เว็บขนาดเล็กไปจนถึงงานองค์กรขนาดใหญ่
ServBay คือสภาพแวดล้อมการพัฒนาเว็บบน macOS ที่รวม PHP, เว็บเซิร์ฟเวอร์หลายชนิด (เช่น Caddy, Nginx), ฐานข้อมูล (เช่น MySQL, PostgreSQL, MongoDB), บริการแคช (เช่น Redis, Memcached) และเครื่องมือพัฒนาอื่น ๆ ไว้ในที่เดียว ServBay ช่วยให้คุณตั้งค่าและจัดการสแตกเหล่านี้ได้สะดวกมาก จึงเหมาะสำหรับการรันโปรเจกต์ PHP ทุกรูปแบบในเครื่อง
เอกสารนี้จะแนะนำคุณตั้งแต่การสร้างโปรเจกต์ Zend Framework (Laminas) ใน ServBay ไปจนถึงการเชื่อมต่อฐานข้อมูลและบริการแคชที่ ServBay ให้มาพร้อม
ข้อกำหนดเบื้องต้น
ก่อนเริ่มใช้งาน โปรดตรวจสอบให้พร้อมดังนี้
- ติดตั้ง ServBay: คุณต้องติดตั้งและเปิดใช้งาน ServBay บน macOS เรียบร้อย หากยังไม่ติดตั้ง ให้ดาวน์โหลดได้ที่ เว็บไซต์ทางการของ ServBay
- ซอฟต์แวร์ใน ServBay: ตรวจสอบให้ติดตั้งและเปิดบริการดังต่อไปนี้ใน ServBay แล้ว
- PHP อย่างน้อย 1 เวอร์ชัน (แนะนำ PHP 8.x ขึ้นไป เพราะ Zend Framework / Laminas ต้องใช้ PHP เวอร์ชันใหม่)
- เว็บเซิร์ฟเวอร์ (Caddy หรือ Nginx)
- Composer (ServBay มักติดตั้งให้อัตโนมัติ)
- บริการฐานข้อมูลที่ต้องใช้ (MySQL, PostgreSQL ฯลฯ) และบริการแคช (Memcached, Redis) สามารถเปิดใช้งานได้จากแผงควบคุมของ ServBay
สร้างโปรเจกต์ Zend Framework
ServBay แนะนำให้เก็บทุกเว็บไซต์ไว้ใน /Applications/ServBay/www
เพื่อให้ระบบจัดการและตั้งค่าได้อัตโนมัติ
เข้าสู่ไดเรกทอรีเว็บไซต์
เปิด Terminal แล้วเข้าไปยังโฟลเดอร์เว็บไซต์
bashcd /Applications/ServBay/www
1สร้างโปรเจกต์ด้วย Composer
ServBay มี Composer ให้พร้อมใช้งาน ไม่ต้องติดตั้งเพิ่ม สามารถสร้างโปรเจกต์ Laminas skeleton ใหม่ด้วยคำสั่งนี้ โดยชุดโค้ดจะถูกสร้างไว้ในโฟลเดอร์
servbay-zend-app
bashcomposer create-project laminas/laminas-skeleton-application servbay-zend-app
1ระบบจะดาวน์โหลดโครงโปรเจกต์ Zend Framework (Laminas) พร้อมทุก dependency ที่จำเป็น ลงในโฟลเดอร์
servbay-zend-app
เข้าสู่ไดเรกทอรีโปรเจกต์
เข้าไปยังโฟลเดอร์โปรเจกต์ที่สร้างใหม่
bashcd servbay-zend-app
1
ตั้งค่าเว็บเซิร์ฟเวอร์ใน ServBay
เพื่อให้เข้าถึง Zend Framework ผ่านเบราว์เซอร์ ต้องเพิ่ม เว็บไซต์ ในแผงควบคุมของ ServBay
- เปิดแผงควบคุม ServBay: เปิดแอป ServBay
- เข้าสู่การตั้งค่าเว็บไซต์: ไปที่แท็บ Website (Websites)
- เพิ่มเว็บไซต์ใหม่: คลิกปุ่ม
+
ด้านล่างซ้ายเพิ่มเว็บไซต์ใหม่ - กรอกข้อมูลเว็บไซต์:
- ชื่อ (Name): ใส่ชื่อที่เข้าใจง่าย เช่น
My Zend Dev Site
- โดเมน (Domain): ใส่โดเมนที่ต้องการใช้เข้าถึง อาจใช้
.local
หรือ.test
เพื่อลดปัญหาโดเมนทับซ้อน เช่นservbay-zend-test.local
(ServBay จะตั้งค่า DNS ในเครื่องให้อัตโนมัติ) - ประเภทเว็บไซต์ (Website Type): เลือก
PHP
- PHP เวอร์ชัน (PHP Version): เลือก PHP เวอร์ชันที่ติดตั้งและต้องการใช้งาน เช่น
8.3
- ไดเรกทอรีราก (Document Root): ต้องกำหนดเป็นโฟลเดอร์
public
ในโปรเจกต์ (index.php
อยู่ที่นี่) เช่น/Applications/ServBay/www/servbay-zend-app/public
- ชื่อ (Name): ใส่ชื่อที่เข้าใจง่าย เช่น
- บันทึกและรีสตาร์ท: คลิกปุ่ม Save (บันทึก) ระบบจะถามว่ายืนยันเปลี่ยนแปลงหรือไม่ ให้คลิกยืนยัน จากนั้นเว็บเซิร์ฟเวอร์จะโหลดคอนฟิกใหม่
ดูรายละเอียดแต่ละขั้นตอนเพิ่มเติมได้ในคู่มือ ServBay หัวข้อ เพิ่มเว็บไซต์แรก
ตัวอย่าง "Hello ServBay!"
เราจะปรับโค้ดให้เมื่อเข้าถึง URL หลัก (/
) จะแสดงข้อความ "Hello ServBay!"
ตั้งค่า Routing และ Controller (module.config.php)
แก้ไขไฟล์
module/Application/config/module.config.php
ให้มีการกำหนด routes และ controller ดังนี้: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', ], ], ], // ... การตั้งค่า route อื่น ๆ ], ], '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'
route และ controller ที่ถูกต้องสร้างหรือแก้ไข Controller (IndexController.php)
แก้ไขหรือสร้างไฟล์
module/Application/src/Controller/IndexController.php
ฟังก์ชันindexAction
จะส่งข้อความไปยัง View:php<?php declare(strict_types=1); namespace Application\Controller; use Laminas\Mvc\Controller\AbstractActionController; use Laminas\View\Model\ViewModel; class IndexController extends AbstractActionController { /** * Default action to display the welcome page. */ public function indexAction() { // ส่งตัวแปร 'message' ไปยัง view 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สร้างหรือแก้ไข View file (index.phtml)
แก้ไขหรือสร้างไฟล์
module/Application/view/application/index/index.phtml
เนื้อหาดังนี้:php<h1><?php echo $this->message; ?></h1>
1ใช้ตัวช่วย View
$this->message
เพื่อแสดงข้อความจาก Controller
เข้าถึงเว็บไซต์
เปิดเบราว์เซอร์แล้วไปที่โดเมนที่ตั้งไว้ เช่น https://servbay-zend-test.local
หากตั้งค่าทุกอย่างถูกต้อง จะเห็นหน้าเว็บแสดง "Hello ServBay!"
แสดงว่า Zend Framework พร้อมใช้งานบน ServBay
ตัวอย่างการเชื่อมต่อฐานข้อมูลและแคช
ServBay ให้บริการฐานข้อมูลและแคชหลายแบบ ตัวอย่างต่อไปนี้สาธิตวิธีเชื่อมต่อและใช้งาน Memcached, Redis, MySQL และ PostgreSQL ภายใน Zend Framework
ข้อควรระวัง: ตัวอย่างเหล่านี้แยกกันใช้งานจริงไม่จำเป็นต้องเปิดใช้ทั้งหมดในโปรเจกต์เดียว สามารถดัดแปลงตามต้องการ และคุณต้องเปิดบริการ (package) ต่าง ๆ จากแผงควบคุม ServBay ให้พร้อมก่อน
ตัวอย่างเชื่อมต่อฐานข้อมูล - สร้างตาราง (Table)
สาธิตการใช้ Laminas DB สร้างตารางในฐานข้อมูลเอง (ไม่ได้ใช้ Migrations tools)
ติดตั้ง Laminas DB
ในโฟลเดอร์โปรเจกต์ ใช้คำสั่งนี้ติดตั้งผ่าน Composer:
bashcomposer require laminas/laminas-db
1สร้างฐานข้อมูลเปล่าด้วยตนเอง
สร้างฐานข้อมูลชื่อ
servbay_zend_app
ใน MySQL หรือ PostgreSQL ผ่าน phpMyAdmin, pgAdmin, MongoDB Compass ฯลฯ โดยตรง ServBay ตั้งค่าเริ่มต้น MySQL/ MariaDB user เป็น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); // นิยาม SQL สำหรับสร้างตาราง users $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 "Executing SQL:\n"; echo $sql->buildSqlString($create, $adapter->getPlatform()) . "\n"; try { // รัน SQL $adapter->query( $sql->buildSqlString($create, $adapter->getPlatform()), Adapter::QUERY_MODE_EXECUTE ); echo "Table 'users' created successfully.\n"; } catch (\Exception $e) { echo "Error creating table: " . $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หมายเหตุ: โค้ดนี้เป็นตัวอย่างเฉพาะกิจ ใช้สร้างตารางแบบ manual เมื่อรันผ่าน PHP CLI:
bashphp create_users_table.php
1
ตัวอย่างเชื่อมต่อ MySQL
ตัวอย่างการเชื่อมต่อและดึงข้อมูลจาก MySQL ใน Controller
ตั้งค่าการเชื่อมต่อ MySQL
แก้ไขไฟล์
config/autoload/global.php
เพิ่ม/ปรับคอนฟิกdb
ดังนี้:php<?php // config/autoload/global.php return [ 'db' => [ 'driver' => 'Pdo_Mysql', 'database' => 'servbay_zend_app', // ชื่อฐานข้อมูล 'username' => 'root', // ชื่อผู้ใช้เริ่มต้น 'password' => 'password', // รหัสเริ่มต้น 'hostname' => '127.0.0.1', 'port' => 3306, // พอร์ต MySQL 'charset' => 'utf8mb4', ], // ... การตั้งค่าอื่น ];
1
2
3
4
5
6
7
8
9
10
11
12
13
14ตั้งค่า Controller Factory (module.config.php)
เพื่อให้ Controller รับ Adapter ผ่าน dependency injection ให้แก้ไฟล์
module/Application/config/module.config.php
ในส่วนcontrollers
(แทนที่หรือเพิ่ม factory) และservice_manager
:php<?php // module/Application/config/module.config.php namespace Application; use Laminas\ServiceManager\Factory\InvokableFactory; // ใช้หากจำเป็น use Laminas\Db\Adapter\AdapterInterface; // เพิ่ม use นี้ return [ // ... คอนฟิกอื่น 'controllers' => [ 'factories' => [ Controller\IndexController::class => function($container) { // ดึง Adapter จาก Service Manager $adapter = $container->get(AdapterInterface::class); return new Controller\IndexController($adapter); }, ], ], 'service_manager' => [ 'aliases' => [ // กำหนด alias สำหรับ AdapterInterface 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
28
29หมายเหตุ: วางคอนฟิกนี้รวมกับค่าดั้งเดิม
ตั้งค่า Routing (module.config.php)
ในไฟล์
module/Application/config/module.config.php
เพิ่ม route ใหม่:php<?php // module/Application/config/module.config.php namespace Application; use Laminas\Router\Http\Literal; return [ 'router' => [ 'routes' => [ // ... routes เดิม '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เพิ่มเมธอดใน Controller (IndexController.php)
เพิ่ม constructor และ action ใหม่ใน
module/Application/src/Controller/IndexController.php
: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 นี้ use Laminas\Db\Sql\Sql; // use นี้ class IndexController extends AbstractActionController { private $adapter; public function __construct(AdapterInterface $adapter) { $this->adapter = $adapter; } public function indexAction() { return new ViewModel([ 'message' => 'Hello ServBay!', ]); } /** * Action เพิ่มยูเซอร์ในตาราง 'users' ด้วย MySQL */ public function mysqlAddAction() { $sql = new Sql($this->adapter); $insert = $sql->insert('users') ->values([ 'name' => 'ServBay Demo User', 'email' => '[email protected]', // อีเมลตัวอย่าง ]); $statement = $sql->prepareStatementForSqlObject($insert); $result = $statement->execute(); $message = $result->getAffectedRows() > 0 ? 'MySQL User added successfully.' : 'Failed to add MySQL user.'; return new ViewModel([ 'message' => $message, ]); } /** * Action แสดงข้อมูล users ทั้งหมดจาก MySQL */ 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 ให้ View 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สร้าง View สำหรับ MySQL
สร้างไฟล์
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 Users</h1> <pre><?php echo $this->users; ?></pre>
1
2ทดสอบใช้งาน MySQL
ตรวจสอบให้แน่ใจว่าเปิด MySQL แล้ว เริ่มจากเข้า
https://servbay-zend-test.local/mysql-add
เพื่อเพิ่ม user จะเห็นข้อความ "MySQL User added successfully." แล้วดูผลลัพธ์ทั้งหมดที่https://servbay-zend-test.local/mysql
ตัวอย่างเชื่อมต่อ PostgreSQL
ตัวอย่างเชื่อมต่อและดึงข้อมูลจาก PostgreSQL
ตั้งค่าการเชื่อมต่อ PostgreSQL
แก้ไขไฟล์
config/autoload/global.php
สลับเป็นค่า 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, // PostgreSQL ], // ... อื่น ๆ ];
1
2
3
4
5
6
7
8
9
10
11
12
13Factory ใน Controller (ดูตัวอย่าง MySQL ข้างต้น)
หากทำตามตัวอย่าง MySQL แล้ว ไม่ต้องเปลี่ยนส่วนนี้อีก
ตั้งค่า Routing (module.config.php)
เพิ่ม route สำหรับ PostgreSQL:
php<?php namespace Application; use Laminas\Router\Http\Literal; return [ 'router' => [ 'routes' => [ // ... routes เก่า '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เพิ่มเมธอดใน Controller (IndexController.php)
เพิ่ม action สำหรับ PostgreSQL:
php<?php // ... use เดิมทั้งหมด class IndexController extends AbstractActionController { private $adapter; public function __construct(AdapterInterface $adapter) { $this->adapter = $adapter; } // ... method เดิม /** * เพิ่ม user ด้วย PostgreSQL */ public function pgsqlAddAction() { $sql = new Sql($this->adapter); $insert = $sql->insert('users') ->values([ 'name' => 'ServBay Demo User', 'email' => '[email protected]', ]); $statement = $sql->prepareStatementForSqlObject($insert); $result = $statement->execute(); $message = $result->getAffectedRows() > 0 ? 'PostgreSQL User added successfully.' : 'Failed to add PostgreSQL user.'; return new ViewModel([ 'message' => $message, ]); } /** * แสดง users จาก PostgreSQL */ 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สร้าง View สำหรับ PostgreSQL
สร้างไฟล์
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 Users</h1> <pre><?php echo $this->users; ?></pre>
1
2ทดสอบใช้งาน PostgreSQL
ตรวจสอบว่า PostgreSQL ทำงานอยู่ เริ่มจาก
https://servbay-zend-test.local/pgsql-add
เพื่อเพิ่ม user แล้วดูผลทั้งหมดที่https://servbay-zend-test.local/pgsql
ตัวอย่างใช้งาน Memcached
สาธิตการแคชข้อมูลด้วย Memcached ใน Controller
ติดตั้ง Memcached Adapter
ใน
composer.json
เพิ่ม dependency: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แล้วรัน
bashcomposer update
1ServBay มี PHP extension
memcached
ให้แล้วตั้งค่า Routing (module.config.php)
เพิ่ม route ใหม่:
php<?php namespace Application; use Laminas\Router\Http\Literal; return [ 'router' => [ 'routes' => [ // ... 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เพิ่มเมธอดใน Controller (IndexController.php)
เพิ่ม memcached action:
php<?php // ... use statements เดิม ... use Laminas\Cache\StorageFactory; use Laminas\Cache\Storage\StorageInterface; class IndexController extends AbstractActionController { // ... methods เดิม ... /** * ตัวอย่างใช้งาน Memcached */ public function memcachedAction() { // เซต Memcached 127.0.0.1:11211 $cache = StorageFactory::factory([ 'adapter' => [ 'name' => 'memcached', 'options' => [ 'servers' => [ ['127.0.0.1', 11211], ], 'ttl' => 300, ], ], 'plugins' => [ 'serializer', 'exception_handler' => ['throw_exceptions' => false], ], ]); $cacheKey = 'my_memcached_data'; $cachedData = $cache->getItem($cacheKey, $success); if (!$success) { $cachedData = 'Hello Memcached! (Data from source, cached at ' . 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สร้าง View Memcached
สร้างไฟล์
module/Application/view/application/index/memcached.phtml
:php<h1>Memcached Example</h1> <p><?php echo $this->message; ?></p>
1
2ทดสอบใช้งาน Memcached
เช็คว่าเปิด Memcached แล้ว เข้า
https://servbay-zend-test.local/memcached
ครั้งแรกจะเห็น "CACHE MISS" ครั้งถัดไปใน 300 วิจะเห็น "CACHE HIT" และเวลาไม่เปลี่ยน
ตัวอย่างใช้งาน Redis
สาธิตการแคชหรือเก็บข้อมูลใน Redis ผ่าน Controller
ติดตั้ง Redis Adapter
ใน
composer.json
เพิ่ม dependency: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แล้วรัน
bashcomposer update
1ServBay มี PHP extension
redis
ให้แล้วตั้งค่า Routing (module.config.php)
เพิ่ม route ใหม่:
php<?php namespace Application; use Laminas\Router\Http\Literal; return [ 'router' => [ 'routes' => [ // ... 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เพิ่มเมธอดใน Controller (IndexController.php)
เพิ่ม redis action:
php<?php // ... use statements เดิม ... use Laminas\Cache\StorageFactory; use Laminas\Cache\Storage\StorageInterface; class IndexController extends AbstractActionController { // ... methods เดิม ... /** * ตัวอย่างใช้งาน Redis */ public function redisAction() { // เซต Redis 127.0.0.1:6379 $cache = StorageFactory::factory([ 'adapter' => [ 'name' => 'redis', 'options' => [ 'server' => [ 'host' => '127.0.0.1', 'port' => 6379, ], 'ttl' => 300, ], ], 'plugins' => [ 'serializer', 'exception_handler' => ['throw_exceptions' => false], ], ]); $cacheKey = 'my_redis_data'; $cachedData = $cache->getItem($cacheKey, $success); if (!$success) { $cachedData = 'Hello Redis! (Data from source, cached at ' . 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สร้าง View Redis
สร้างไฟล์
module/Application/view/application/index/redis.phtml
:php<h1>Redis Example</h1> <p><?php echo $this->message; ?></p>
1
2ทดสอบใช้งาน Redis
เช็คว่าเปิด Redis Service แล้ว เข้า
https://servbay-zend-test.local/redis
ครั้งแรกจะเห็น "CACHE MISS" ครั้งถัดไปจะเห็น "CACHE HIT" และเวลาเดิม
สรุป
เมื่อทำครบทุกขั้นตอนนี้ คุณจะสร้างและตั้งค่าโปรเจกต์ Zend Framework (Laminas) บน ServBay ได้สำเร็จ พร้อมเข้าใช้งานผ่าน เว็บไซต์ ในแผงควบคุม และเชื่อมต่อกับ MySQL, PostgreSQL, Memcached, Redis ได้สะดวก
ServBay ทำให้งานเซ็ตอัพสภาพแวดล้อมท้องถิ่นของนักพัฒนาเป็นเรื่องง่าย คุณจึงโฟกัสที่การเขียนโค้ดได้เต็มที่ และยังปรับแต่งได้เหมือน production มากที่สุด เพิ่ม Productivity ได้อย่างมีประสิทธิภาพ