Membuat dan Menjalankan Proyek Zend Framework (Laminas) di ServBay
Ringkasan
Zend Framework (sekarang bagian dari Laminas Project) adalah framework PHP open source yang canggih, menyediakan serangkaian komponen berorientasi objek berkualitas tinggi untuk membangun aplikasi dan layanan web modern. Framework ini terkenal karena fleksibilitas, desain modular, serta performa tinggi—menjadikannya pilihan ideal untuk membangun situs sederhana maupun aplikasi skala enterprise yang kompleks.
ServBay adalah lingkungan pengembangan web lokal yang dirancang untuk macOS dan Windows. Platform ini mengintegrasikan PHP, berbagai web server (seperti Caddy dan Nginx), database (seperti MySQL, PostgreSQL, MongoDB), layanan cache (Redis, Memcached), serta berbagai alat pengembangan lain. ServBay memberikan kemudahan dalam mengonfigurasi dan mengelola paket perangkat lunak tersebut, sehingga proses setup dan menjalankan berbagai proyek framework PHP secara lokal menjadi sederhana.
Dokumen ini akan membimbing Anda dalam membuat dan menjalankan proyek Zend Framework (Laminas) di ServBay, serta demonstrasi integrasi database dan layanan cache yang tersedia di ServBay.
Prasyarat
Sebelum memulai, pastikan Anda telah melakukan hal berikut:
- Instalasi ServBay: ServBay sudah terinstal dan berjalan di macOS atau Windows Anda. Jika belum, kunjungi Situs Resmi ServBay untuk tutorial unduh dan instalasi.
- Paket ServBay: Pastikan paket perangkat lunak yang diperlukan telah diinstal dan aktif di ServBay, meliputi:
- Minimal satu versi PHP (disarankan PHP 8.x atau lebih baru, karena Laminas biasanya membutuhkan versi PHP yang terbaru).
- Web server (Caddy atau Nginx).
- Composer (biasanya sudah termasuk dalam ServBay).
- Database (misalnya MySQL, PostgreSQL) dan layanan cache (misal Memcached, Redis) yang akan digunakan—semua dapat dinyalakan melalui panel kontrol ServBay.
Membuat Proyek Zend Framework
ServBay merekomendasikan agar semua proyek website Anda ditempatkan pada direktori berikut untuk memudahkan pengelolaan dan konfigurasi otomatis:
- macOS:
/Applications/ServBay/www
- Windows:
C:\ServBay\www
Masuk ke direktori root website
Buka terminal kemudian navigasikan ke root website seperti yang direkomendasikan ServBay:
macOS:
bashcd /Applications/ServBay/www
1Windows:
cmdcd C:\ServBay\www
1Membuat proyek dengan Composer
ServBay sudah menyertakan Composer, sehingga tidak perlu instalasi terpisah. Gunakan perintah
create-project
dari Composer untuk membuat aplikasi skeleton Laminas baru. Misalnya, buat proyek dalam subfolderservbay-zend-app
:bashcomposer create-project laminas/laminas-skeleton-application servbay-zend-app
1Perintah ini akan mengunduh skeleton aplikasi Zend Framework (Laminas) ke dalam folder tersebut beserta dependensi yang dibutuhkan.
Masuk ke direktori proyek
Navigasikan ke direktori proyek yang baru dibuat:
bashcd servbay-zend-app
1
Konfigurasi Web Server
Agar proyek Zend Framework Anda dapat diakses lewat browser, Anda perlu membuat konfigurasi website di ServBay.
- Buka panel kontrol ServBay: Jalankan aplikasi ServBay.
- Masuk ke pengaturan website: Pilih tab Website (Websites) pada panel ServBay.
- Tambahkan website baru: Klik tombol
+
di kiri bawah untuk membuat konfigurasi baru. - Isi detail website:
- Name: Nama yang mudah dikenali, misal
My Zend Dev Site
. - Domain: Domain yang akan digunakan di browser. Untuk keamanan, gunakan akhiran
.local
atau.test
—misalservbay-zend-test.local
. ServBay akan otomatis mengatur DNS lokal. - Website Type: Pilih
PHP
. - PHP Version: Pilih versi PHP yang diinginkan (misal
8.3
). Pastikan versi sudah diinstal dan aktif di ServBay. - Document Root (Root Website): Ini adalah folder yang diakses web server. Pada Zend Framework, file entrypoint
index.php
ada di folderpublic
. Atur root kepublic
dalam proyek Anda:/Applications/ServBay/www/servbay-zend-app/public
.
- Name: Nama yang mudah dikenali, misal
- Simpan dan restart: Klik Save. ServBay akan meminta konfirmasi perubahan, lalu web server akan reload sehingga website baru aktif.
Lihat bab Menambah Website Pertama pada dokumen ServBay untuk detail konfigurasi.
Contoh Dasar "Hello ServBay!"
Mari kita ubah kode agar saat mengakses URL root (/
), akan tampil "Hello ServBay!".
Konfigurasi routing dan controller (module.config.php)
Edit file
module/Application/config/module.config.php
di root proyek. Pastikan konfigurasi berikut sudah ada: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', ], ], ], // ... konfigurasi routing lainnya ], ], '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', ], ], // ... konfigurasi lainnya ];
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
49Catatan: Potongan kode di atas adalah bagian dari file
module.config.php
. Pastikan routing'home'
serta factory untukController\IndexController::class
sudah terdapat.Buat atau ubah controller (IndexController.php)
Edit atau buat file
module/Application/src/Controller/IndexController.php
. MetodeindexAction
mengirimkan pesan ke view dengan 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 untuk menampilkan halaman selamat datang. */ public function indexAction() { // Mengembalikan ViewModel dan mengirim variabel 'message' ke view return new ViewModel([ 'message' => 'Hello ServBay!', ]); } // ... metode action lain }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24Buat atau ubah file view (index.phtml)
Edit atau buat file
module/Application/view/application/index/index.phtml
. File ini menerima variabelmessage
dari controller dan menampilkannya:php<h1><?php echo $this->message; ?></h1>
1View helper
$this->message
digunakan untuk menampilkan data dari controller.
Mengakses Website
Buka browser Anda dan akses domain yang sudah dikonfigurasi di ServBay, contoh: https://servbay-zend-test.local
.
Jika konfigurasi sudah tepat, Anda akan melihat halaman dengan pesan Hello ServBay!
. Ini menandakan proyek Zend Framework sudah berjalan di ServBay.
Contoh Integrasi Database dan Cache
ServBay mendukung berbagai layanan database dan cache. Berikut demonstrasi menghubungkan dan menggunakan Memcached, Redis, MySQL, serta PostgreSQL di proyek Zend Framework.
Penting: Semua contoh berikut adalah demonstrasi mandiri. Di aplikasi nyata, Anda biasanya memilih satu jenis database dan satu atau beberapa layanan cache sesuai kebutuhan, serta mengelola koneksi menggunakan dependency injection dan best practice lainnya. Jalankan contoh-contoh ini hanya jika layanan terkait (MySQL, PostgreSQL, Memcached, Redis) sudah aktif di ServBay.
Contoh Interaksi Database - Membuat Tabel
Pertama-tama, kita buat tabel di database menggunakan komponen Laminas DB. Contoh berikut mendemonstrasikan cara manual mendefinisikan dan menjalankan operasi pembuatan tabel tanpa menggunakan tool migration penuh dari Laminas.
Instalasi Laminas DB
Di root proyek, jalankan perintah Composer:
bashcomposer require laminas/laminas-db
1Buat database manual
Sebelum menjalankan contoh, buat database
servbay_zend_app
di layanan database ServBay. Anda bisa gunakan phpMyAdmin, pgAdmin, atau alat lain dari panel ServBay. Default username untuk MySQL/MariaDB adalahroot
, passwordpassword
. PostgreSQL pun default-nyaroot
danpassword
.Eksekusi skrip pembuatan tabel (contoh)
Buat file PHP (misal
create_users_table.php
) di root proyek atau folder sementara, berisi:php<?php // create_users_table.php use Laminas\Db\Adapter\Adapter; use Laminas\Db\Sql\Sql; // Misal menggunakan MySQL atau MariaDB $adapter = new Adapter([ 'driver' => 'Pdo_Mysql', // Atau 'Pdo_Pgsql' 'database' => 'servbay_zend_app', 'username' => 'root', 'password' => 'password', // Password ServBay default 'hostname' => '127.0.0.1', // 'port' => 3306, // Default MySQL // 'port' => 5432, // Default PostgreSQL ]); $sql = new Sql($adapter); // Definisi SQL pembuatan tabel 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 "Eksekusi SQL:\n"; echo $sql->buildSqlString($create, $adapter->getPlatform()) . "\n"; try { // Eksekusi SQL $adapter->query( $sql->buildSqlString($create, $adapter->getPlatform()), Adapter::QUERY_MODE_EXECUTE ); echo "Tabel 'users' berhasil dibuat.\n"; } catch (\Exception $e) { echo "Gagal membuat tabel: " . $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
38Catatan: Skrip di atas hanya untuk eksekusi manual. Di aplikasi produksi, sebaiknya gunakan tool migrasi resmi.
Jalankan dengan CLI PHP untuk membuat tabel:
bashphp create_users_table.php
1
Contoh Integrasi MySQL
Demonstrasi menghubungkan dan melakukan query pada database MySQL dari controller Zend Framework.
Konfigurasi koneksi database
Edit file
config/autoload/global.php
dan masukkan detail database MySQL:php<?php // config/autoload/global.php return [ 'db' => [ 'driver' => 'Pdo_Mysql', 'database' => 'servbay_zend_app', // Pastikan sudah ada 'username' => 'root', // Default ServBay 'password' => 'password', // Default ServBay 'hostname' => '127.0.0.1', 'port' => 3306, 'charset' => 'utf8mb4', ], // ... konfigurasi lain ];
1
2
3
4
5
6
7
8
9
10
11
12
13
14Konfigurasi pabrik controller (module.config.php)
Agar Adapter bisa di-inject ke controller, ubah definisi factory untuk
IndexController
dimodule/Application/config/module.config.php
:php<?php // module/Application/config/module.config.php namespace Application; use Laminas\ServiceManager\Factory\InvokableFactory; // Jika InvokableFactory masih diperlukan use Laminas\Db\Adapter\AdapterInterface; return [ // ... konfigurasi lain '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, ], ], // ... konfigurasi lain ];
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
27Catatan: Pastikan bagian
service_manager
sudah sesuai.Konfigurasi routing (module.config.php)
Tambahkan routing baru untuk endpoint MySQL pada
'routes'
:php<?php // module/Application/config/module.config.php namespace Application; use Laminas\Router\Http\Literal; return [ 'router' => [ 'routes' => [ // ... routing lain (misal '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', ], ], ], ], ], // ... konfigurasi lain ];
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
34Tambahkan metode controller (IndexController.php)
Pada file
module/Application/src/Controller/IndexController.php
, tambahkan konstruktor serta dua action untuk 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 */ public function indexAction() { return new ViewModel([ 'message' => 'Hello ServBay!', ]); } /** * Action untuk menambah user ke tabel 'users' via 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 ? 'User MySQL berhasil ditambahkan.' : 'Gagal menambah user MySQL.'; return new ViewModel([ 'message' => $message, ]); } /** * Action untuk mengambil semua user di tabel 'users' melalui 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 lain }
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
75Buat file view
File
module/Application/view/application/index/mysql-add.phtml
:php<h1><?php echo $this->message; ?></h1>
1File
module/Application/view/application/index/mysql.phtml
:php<h1>MySQL Users</h1> <pre><?php echo $this->users; ?></pre>
1
2Akses contoh MySQL
Pastikan MySQL sudah aktif di ServBay. Akses
https://servbay-zend-test.local/mysql-add
untuk menambah user. Anda akan melihat pesan "User MySQL berhasil ditambahkan.". Selanjutnya akseshttps://servbay-zend-test.local/mysql
untuk melihat data user dalam format JSON.
Contoh Integrasi PostgreSQL
Langkah-langkahnya mirip dengan MySQL, hanya detail konfigurasi dan endpoint yang berbeda.
Konfigurasi koneksi database
Di file
config/autoload/global.php
, ubah ke konfigurasi 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, ], // ... konfigurasi lain ];
1
2
3
4
5
6
7
8
9
10
11
12
13Konfigurasi pabrik controller
Pastikan factory Adapter di controller sudah seperti pada contoh MySQL.
Konfigurasi routing (module.config.php)
Tambah routing untuk endpoint PostgreSQL:
php<?php namespace Application; use Laminas\Router\Http\Literal; return [ 'router' => [ 'routes' => [ // ... routing lainnya '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', ], ], ], ], ], // ... konfigurasi lain ];
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
33Tambahkan metode controller (IndexController.php)
Tambahkan metode berikut untuk endpoint 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 lain /** * Action untuk menambah user ke 'users' via 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 ? 'User PostgreSQL berhasil ditambahkan.' : 'Gagal menambah user PostgreSQL.'; return new ViewModel([ 'message' => $message, ]); } /** * Action untuk mengambil semua user di tabel 'users' melalui 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
65Buat file view
File
module/Application/view/application/index/pgsql-add.phtml
:php<h1><?php echo $this->message; ?></h1>
1File
module/Application/view/application/index/pgsql.phtml
:php<h1>PostgreSQL Users</h1> <pre><?php echo $this->users; ?></pre>
1
2Akses contoh PostgreSQL
Pastikan layanan PostgreSQL telah berjalan. Akses
https://servbay-zend-test.local/pgsql-add
untuk menambah data, kemudian akseshttps://servbay-zend-test.local/pgsql
untuk menampilkan daftar user.
Contoh Integrasi Memcached
Memcached digunakan untuk caching data di controller.
Instal Adapter Memcached
Di file
composer.json
, tambahkan dependensi:json{ "require": { "laminas/laminas-skeleton-application": "^1.0", "laminas/laminas-cache-storage-adapter-memcached": "^2.0" } }
1
2
3
4
5
6Jalankan
composer update
. ServBay sudah dilengkapi ekstensi PHPmemcached
.Konfigurasi routing (module.config.php)
Tambahkan endpoint pada routing:
php<?php namespace Application; use Laminas\Router\Http\Literal; return [ 'router' => [ 'routes' => [ // ... routing lainnya 'memcached' => [ 'type' => Literal::class, 'options' => [ 'route' => '/memcached', 'defaults' => [ 'controller' => Controller\IndexController::class, 'action' => 'memcached', ], ], ], ], ], // ... konfigurasi lain ];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23Tambahkan metode controller (IndexController.php)
Tambahkan action berikut:
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 { // ... konstruktor dan action lain /** * Action demonstrasi penggunaan Memcached. */ public function memcachedAction() { // Membuat instance storage Memcached // Default alamat Memcached ServBay adalah 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 dari sumber, disimpan pada ' . 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
54Buat file view
File untuk endpoint
/memcached
:php<h1>Memcached Example</h1> <p><?php echo $this->message; ?></p>
1
2Akses contoh Memcached
Pastikan Memcached aktif di ServBay. Akses
https://servbay-zend-test.local/memcached
. Pertama kali, Anda akan melihat pesan dengan "CACHE MISS". Dalam masa berlaku cache (300 detik), akses lagi akan menampilkan "CACHE HIT" dengan waktu yang tidak berubah.
Contoh Integrasi Redis
Redis dapat digunakan untuk caching atau penyimpanan data serupa.
Instal Adapter Redis
Pada
composer.json
, tambahkan:json{ "require": { "laminas/laminas-skeleton-application": "^1.0", "laminas/laminas-cache-storage-adapter-redis": "^2.0", "ext-redis": "*" } }
1
2
3
4
5
6
7Jalankan
composer update
. ServBay sudah menyediakan ekstensiredis
.Konfigurasi routing (module.config.php)
Tambahkan endpoint Redis:
php<?php namespace Application; use Laminas\Router\Http\Literal; return [ 'router' => [ 'routes' => [ // ... routing lainnya 'redis' => [ 'type' => Literal::class, 'options' => [ 'route' => '/redis', 'defaults' => [ 'controller' => Controller\IndexController::class, 'action' => 'redis', ], ], ], ], ], // ... konfigurasi lain ];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23Tambahkan metode controller (IndexController.php)
Action untuk 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 { // ... konstruktor dan action lain /** * Action demonstrasi penggunaan Redis. */ public function redisAction() { // Default Redis ServBay di 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! (Data dari sumber, disimpan pada ' . 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
56Buat file view
Untuk endpoint Redis:
php<h1>Redis Example</h1> <p><?php echo $this->message; ?></p>
1
2Akses contoh Redis
Pastikan Redis aktif di ServBay. Akses
https://servbay-zend-test.local/redis
. Data yang pertama kali diakses akan menampilkan "CACHE MISS", sementara akses berikutnya dalam masa berlaku cache akan menampilkan "CACHE HIT".
Kesimpulan
Dengan mengikuti langkah-langkah di atas, Anda telah berhasil membuat, mengonfigurasi, dan menjalankan proyek Zend Framework (Laminas) di lingkungan pengembangan lokal ServBay. Anda telah belajar mengatur website dan web server di ServBay untuk mengarah ke direktori publik proyek, serta cara mengintegrasikan database MySQL, PostgreSQL, dan layanan cache Memcached serta Redis ke dalam aplikasi Anda.
ServBay menyederhanakan setup dan manajemen lingkungan pengembangan lokal, sehingga Anda bisa lebih fokus mengembangkan aplikasi. Manfaatkan berbagai paket dan konfigurasi fleksibel ServBay untuk mensimulasikan lingkungan produksi secara lokal serta meningkatkan produktivitas coding Anda.