สร้างและรันโปรเจกต์ Zend Framework (Laminas) บน ServBay
ภาพรวม
Zend Framework (ปัจจุบันเป็นส่วนหนึ่งของ Laminas Project) คือเฟรมเวิร์ก PHP แบบโอเพ่นซอร์สที่ทรงพลัง ให้ชุดคอมโพเนนต์เชิงวัตถุคุณภาพสูงสำหรับการสร้างเว็บแอปพลิเคชันและบริการสมัยใหม่ โดดเด่นด้วยความยืดหยุ่น โครงสร้างแบบโมดูล และประสิทธิภาพสูง เหมาะกับการสร้างทั้งเว็บไซท์ง่ายๆ ไปจนถึงแอปพลิเคชันระดับองค์กร
ServBay คือสภาพแวดล้อมพัฒนาเว็บบนเครื่องที่สร้างมาเพื่อ macOS และ Windows ประกอบด้วย PHP, เว็บเซิร์ฟเวอร์หลายประเภท (เช่น Caddy และ Nginx), ฐานข้อมูลหลายชนิด (MySQL, PostgreSQL, MongoDB), บริการแคช (Redis, Memcached) รวมถึงเครื่องมือสำหรับนักพัฒนาอื่นๆ ServBay มอบวิธีการบริหารจัดการซอฟต์แวร์ทั้งหมดเหล่านี้ได้ง่าย ช่วยให้ติดตั้งและรันโปรเจกต์ PHP ได้อย่างรวดเร็วและสะดวก
คู่มือนี้จะแนะนำวิธีสร้างและรันโปรเจกต์ Zend Framework (Laminas) บน ServBay พร้อมตัวอย่างการเชื่อมต่อฐานข้อมูลและบริการแคชใน ServBay
ข้อกำหนดเบื้องต้น
ก่อนเริ่มต้น กรุณาตรวจสอบว่าคุณได้เตรียมสิ่งต่อไปนี้แล้ว:
- ติดตั้ง ServBay: ต้องติดตั้ง ServBay บน macOS หรือ Windows และพร้อมใช้งาน หากยังไม่ได้ติดตั้ง ไปที่ เว็บไซต์ ServBay เพื่อดูวิธีดาวน์โหลดและติดตั้ง
- แพ็กเกจซอฟต์แวร์ ServBay: ตรวจสอบว่าได้ติดตั้งและเปิดใช้งานแพ็กเกจที่ต้องการใน ServBay ได้แก่:
- PHP (แนะนำเวอร์ชัน 8.x ขึ้นไป)
- เว็บเซิร์ฟเวอร์ (Caddy หรือ Nginx)
- Composer (ServBay มาพร้อมอยู่แล้ว)
- ฐานข้อมูลและบริการแคช (เช่น MySQL, PostgreSQL, Memcached, Redis) สามารถเปิดใช้งานผ่านแผงควบคุม ServBay
สร้างโปรเจกต์ Zend Framework
ServBay แนะนำให้จัดเก็บโปรเจกต์เว็บไซต์ไว้ในไดเรกทอรีต่อไปนี้ เพื่อให้บริหารจัดการและเชื่อมต่อกับเว็บไซต์ใน ServBay ได้สะดวก:
- macOS:
/Applications/ServBay/www
- Windows:
C:\ServBay\www
เข้าไดเรกทอรีรากของเว็บไซต์
เปิดแอปเทอร์มินัลแล้วเข้าไปยังไดเรกทอรีที่ ServBay แนะนำ:
macOS:
bashcd /Applications/ServBay/www
1Windows:
cmdcd C:\ServBay\www
1สร้างโปรเจกต์ด้วย Composer
Composer ติดมาพร้อมกับ ServBay แล้ว ใช้คำสั่ง
create-project
เพื่อสร้างโปรเจกต์ Laminas skeleton application ใหม่ในโฟลเดอร์ย่อยชื่อservbay-zend-app
:bashcomposer create-project laminas/laminas-skeleton-application servbay-zend-app
1คำสั่งนี้จะดาวน์โหลดโครงโปรเจกต์ Laminas พร้อม dependencies ทั้งหมดเข้าไปในโฟลเดอร์
servbay-zend-app
เข้าโฟลเดอร์โปรเจกต์
ไปยังโฟลเดอร์โปรเจกต์ที่เพิ่งสร้าง:
bashcd servbay-zend-app
1
ตั้งค่าเว็บเซิร์ฟเวอร์
ในการเข้าถึงโปรเจกต์ Zend Framework ผ่านเว็บเบราว์เซอร์ ต้องสร้างเว็บไซต์ใน ServBay
- เปิดแผงควบคุม ServBay: เปิดแอป ServBay
- เข้าเมนูเว็บไซต์: คลิกที่แท็บ เว็บไซท์ (Websites) ในแผงควบคุม ServBay
- เพิ่มเว็บไซต์ใหม่: คลิกปุ่ม
+
ล่างซ้ายเพื่อเพิ่มเว็บไซต์ - กรอกข้อมูลเว็บไซต์:
- ชื่อ (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
ของโปรเจกต์:/Applications/ServBay/www/servbay-zend-app/public
- ชื่อ (Name): ตั้งชื่อเว็บไซต์ให้ง่ายต่อการจำ (เช่น
- บันทึกและรีสตาร์ท: กด บันทึก (Save) ServBay จะถามเพื่อยืนยันการเปลี่ยนแปลง เมื่อยืนยันแล้วเว็บเซิร์ฟเวอร์จะโหลด config ใหม่และเว็บไซต์จะพร้อมใช้งาน
ดูวิธีตั้งค่าเพิ่มเติมได้ที่หัวข้อ การเพิ่มเว็บไซต์แรก ในเอกสาร ServBay
ตัวอย่าง "Hello ServBay!"
ต่อไปจะเป็นตัวอย่างการเปลี่ยนโค้ดโปรเจกต์ให้โชว์ข้อความ "Hello ServBay!" เมื่อเข้าหน้าแรก (/
)
ตั้งค่าระบบ Routing และ Controller (module.config.php)
แก้ไฟล์
module/Application/config/module.config.php
ให้มีการตั้งค่า routing และ 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', ], ], ], // ... 其他路由配置 ], ], '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
กรุณารวมเข้า config เดิม และแน่ใจว่ามี route'home'
กับ factory สำหรับController\IndexController::class
สร้างหรือแก้ Controller (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 { /** * Default action to display the welcome page. */ public function indexAction() { // คืน ViewModel และส่ง '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 (index.phtml)
ทำไฟล์
module/Application/view/application/index/index.phtml
ซึ่งจะแสดงข้อความจาก controller:php<h1><?php echo $this->message; ?></h1>
1ใช้ view helper
$this->message
ใน Laminas เพื่อเชื่อมต่อกับข้อมูลจาก controller
เข้าใช้งานเว็บไซต์
เปิดเว็บเบราว์เซอร์ เข้าโดเมนที่ตั้งไว้ใน ServBay (เช่น https://servbay-zend-test.local
)
หากตั้งค่าทุกอย่างถูกต้อง จะเห็นข้อความ Hello ServBay!
แสดงบนหน้าเว็บ นั่นหมายความว่าโปรเจกต์ Zend Framework ของคุณทำงานได้สำเร็จบน ServBay
ตัวอย่างการเชื่อมต่อฐานข้อมูลและแคช
ServBay มีฐานข้อมูลและบริการแคชหลากหลาย ต่อไปนี้เป็นตัวอย่างการใช้ Memcached, Redis, MySQL และ PostgreSQL ร่วมกับโปรเจกต์ Zend Framework
ข้อสำคัญ: ตัวอย่างแต่ละอันเป็นการสาธิตแยกต่างหาก เมื่อพัฒนาแอปจริงจะเลือกใช้เพียงบางบริการตามความต้องการ และเชื่อมต่อแบบ Dependency Injection ตรวจสอบให้แน่ใจว่าบริการแต่ละตัว (MySQL, PostgreSQL, Memcached, Redis) เปิดทำงานใน ServBay แล้วก่อนลองตัวอย่าง
ตัวอย่างการเชื่อมต่อฐานข้อมูล - การสร้างตาราง
ตัวอย่างการสร้างตาราง database โดยใช้ Laminas DB
ติดตั้ง Laminas DB
ติดตั้งผ่าน Composer:
bashcomposer require laminas/laminas-db
1สร้างฐานข้อมูลด้วยตนเอง
ก่อนรันตัวอย่างฐานข้อมูล สร้างฐานข้อมูลชื่อ
servbay_zend_app
ใน ServBay โดยใช้เครื่องมือจัดการเช่น phpMyAdmin, pgAdmin, MongoDB Compass เป็นต้น สำหรับ MySQL/MariaDB ชื่อผู้ใช้และรหัสผ่านเริ่มต้นคือroot
และpassword
PostgreSQL ก็ใช้root
กับpassword
เช่นกันนิยามและรันสคริปต์สร้างตาราง (ตัวอย่าง)
สร้างไฟล์ PHP (เช่น
create_users_table.php
) แล้ววางโค้ดนี้ไว้: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หมายเหตุ: สคริปต์นี้ใช้ run แบบ manual ปกติจะใช้เครื่องมือ Laminas Migrations ในโปรเจกต์จริง
ใช้ PHP CLI รันสคริปต์:
bashphp create_users_table.php
1
ตัวอย่างการเชื่อมต่อ MySQL
วิธีใช้งาน MySQL ใน controller ของ Zend Framework
ตั้งค่าการเชื่อมต่อฐานข้อมูล
แก้ไขไฟล์
config/autoload/global.php
: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, 'charset' => 'utf8mb4', ], // ... config อื่นๆ ];
1
2
3
4
5
6
7
8
9
10
11
12
13
14ตั้งค่า Controller Factory (module.config.php)
นิยาม factory สำหรับฉีด
Laminas\Db\Adapter\Adapter
เข้า IndexController ในmodule/Application/config/module.config.php
ดังนี้:php<?php // module/Application/config/module.config.php namespace Application; use Laminas\ServiceManager\Factory\InvokableFactory; use Laminas\Db\Adapter\AdapterInterface; return [ // ... config อื่นๆ 'controllers' => [ 'factories' => [ Controller\IndexController::class => function($container) { $adapter = $container->get(AdapterInterface::class); return new Controller\IndexController($adapter); }, // เพิ่ม factory สำหรับ controller อื่นถ้าต้องการ ], ], 'service_manager' => [ 'aliases' => [ AdapterInterface::class => 'Laminas\Db\Adapter\Adapter', ], 'factories' => [ 'Laminas\Db\Adapter\Adapter' => \Laminas\Db\Adapter\AdapterServiceFactory::class, ], ], // ... config อื่นๆ ];
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หมายเหตุ: ต้องรวมเข้า config เดิม โดยเฉพาะส่วน
service_manager
ให้แน่ใจว่าตั้ง alias และ factory ไว้ครบถ้วนตั้งค่า route (module.config.php)
เพิ่ม route สำหรับ MySQL ที่
module/Application/config/module.config.php
:php<?php // module/Application/config/module.config.php namespace Application; use Laminas\Router\Http\Literal; return [ 'router' => [ 'routes' => [ // ... route เดิม เช่น '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', ], ], ], ], ], // ... config อื่นๆ (controllers, service_manager, view_manager เป็นต้น) ];
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 เพื่อรับ adapter และเมธอดสำหรับ MySQL:
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; } /** * Default action to display the welcome page. */ public function indexAction() { return new ViewModel([ 'message' => 'Hello ServBay!', ]); } /** * เพิ่มผู้ใช้ในตาราง 'users' ด้วย MySQL */ 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 User added successfully.' : 'Failed to add MySQL user.'; return new ViewModel([ 'message' => $message, ]); } /** * ดึงผู้ใช้ทั้งหมดจาก '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; } 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สร้างไฟล์ View
สร้างไฟล์
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 ใน ServBay เปิดทำงานอยู่ เข้าทาง URL
https://servbay-zend-test.local/mysql-add
เพื่อเพิ่มผู้ใช้ใหม่ จะเห็นข้อความ "MySQL User added successfully." จากนั้นเข้าhttps://servbay-zend-test.local/mysql
เพื่อดูข้อมูลผู้ใช้เป็น JSON
ตัวอย่างการเชื่อมต่อ PostgreSQL
เชื่อมต่อฐานข้อมูล PostgreSQL ผ่าน controller ใน Zend Framework
ตั้งค่าการเชื่อมต่อฐานข้อมูล
แก้ไข
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, ], // ... config อื่นๆ ];
1
2
3
4
5
6
7
8
9
10
11
12
13ตั้งค่า Controller Factory (module.config.php)
ใช้ config เหมือนของ MySQL หากได้ปรับไว้แล้ว ไม่ต้องแก้เพิ่มเติม
ตั้งค่า route (module.config.php)
เพิ่ม route สำหรับ PostgreSQL:
php<?php // module/Application/config/module.config.php namespace Application; use Laminas\Router\Http\Literal; return [ 'router' => [ 'routes' => [ // ... route เดิม เช่น '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', ], ], ], ], ], // ... config อื่นๆ ];
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)
เพิ่มเมธอดสำหรับ PostgreSQL:
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 /** * เพิ่มผู้ใช้ใน 'users' ด้วย PostgreSQL */ 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 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
59
60
61
62
63
64
65สร้างไฟล์ View
สร้างไฟล์
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 ใน ServBay เปิดทำงานอยู่ เข้า
https://servbay-zend-test.local/pgsql-add
เพื่อเพิ่ม user จะเห็น "PostgreSQL User added successfully." แล้วเข้าhttps://servbay-zend-test.local/pgsql
เพื่อดูข้อมูล user เป็น JSON
ตัวอย่างการเชื่อมต่อ Memcached
ใช้ Memcached ใน controller เพื่อแคชข้อมูล
ติดตั้ง Memcached Adapter
ใน composer.json:
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
เพื่อดึง dependency ServBay มี PHP extensionmemcached
มาให้แล้วตั้งค่า route (module.config.php)
เพิ่ม route สำหรับ Memcached:
php<?php // module/Application/config/module.config.php namespace Application; use Laminas\Router\Http\Literal; return [ 'router' => [ 'routes' => [ // ... route เดิม 'memcached' => [ 'type' => Literal::class, 'options' => [ 'route' => '/memcached', 'defaults' => [ 'controller' => Controller\IndexController::class, 'action' => 'memcached', ], ], ], ], ], // ... config อื่นๆ ];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24เพิ่มเมธอดใน Controller (IndexController.php)
เพิ่มเมธอดสำหรับ Memcached:
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 { // ... constructor และ 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! (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
50
51
52
53
54สร้างไฟล์ View
สร้างไฟล์
module/Application/view/application/index/memcached.phtml
:php<h1>Memcached Example</h1> <p><?php echo $this->message; ?></p>
1
2เข้าใช้งานตัวอย่าง Memcached
ตรวจสอบว่าบริการ Memcached ใน ServBay เปิดทำงานอยู่ เข้า
https://servbay-zend-test.local/memcached
ครั้งแรกจะเห็น "CACHE MISS" หากรีโหลดในเวลาแคชจะเห็น "CACHE HIT" และ timestamp ไม่เปลี่ยนแปลง แสดงว่าข้อมูลมาจากแคช
ตัวอย่างการเชื่อมต่อ Redis
ใช้ Redis ใน controller สำหรับการ cache หรือเก็บข้อมูล
ติดตั้ง Redis Adapter
ใน composer.json:
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
ติดตั้ง dependency ServBay มี PHP extensionredis
อยู่แล้วตั้งค่า route (module.config.php)
เพิ่ม route สำหรับ Redis:
php<?php // module/Application/config/module.config.php namespace Application; use Laminas\Router\Http\Literal; return [ 'router' => [ 'routes' => [ // ... route เดิม 'redis' => [ 'type' => Literal::class, 'options' => [ 'route' => '/redis', 'defaults' => [ 'controller' => Controller\IndexController::class, 'action' => 'redis', ], ], ], ], ], // ... config อื่นๆ ];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24เพิ่มเมธอดใน Controller (IndexController.php)
เพิ่มเมธอดสำหรับ Redis:
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 { // ... constructor และ 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, ], 'ttl' => 300, // อายุแคช 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
50
51
52
53
54
55สร้างไฟล์ View
สร้างไฟล์
module/Application/view/application/index/redis.phtml
:php<h1>Redis Example</h1> <p><?php echo $this->message; ?></p>
1
2เข้าใช้งานตัวอย่าง Redis
ตรวจสอบว่า Redis เปิดใช้งานใน ServBay เข้า
https://servbay-zend-test.local/redis
ครั้งแรกจะโชว์ "CACHE MISS" หากรีโหลดภายในอายุแคชจะเห็น "CACHE HIT"
สรุป
เมื่อครบขั้นตอนนี้ คุณจะสามารถสร้าง, ตั้งค่า, และรันโปรเจกต์ Zend Framework (Laminas) บนสภาพแวดล้อม ServBay ได้สำเร็จ รวมถึงตั้งค่าเว็บไซต์ใน ServBay ให้ชี้ไปที่ public directory ของโปรเจกต์ และเชื่อมต่อใช้งาน MySQL, PostgreSQL, Memcached และ Redis ได้อย่างสมบูรณ์
ServBay ช่วยให้งานตั้งค่าสภาพแวดล้อมพัฒนาบนเครื่องง่ายและสะดวก คุณสามารถเน้นพัฒนาโค้ดและโปรเจกต์ เพิ่มประสิทธิภาพงานด้วยซอฟต์แวร์และ config ที่ครบใน ServBay พร้อมจำลองการทำงานแบบ production ได้อย่างมืออาชีพ