Membuat dan Menjalankan Proyek Zend Framework (Laminas) di ServBay
Gambaran Umum
Zend Framework (kini sebagai bagian dari Laminas Project) adalah framework PHP open source yang kuat, menawarkan kumpulan komponen berorientasi objek berkualitas tinggi untuk membangun aplikasi dan layanan web modern. Framework ini dikenal fleksibel, modular, dan berkinerja tinggi—pilihan ideal mulai dari website sederhana hingga aplikasi perusahaan yang kompleks.
ServBay adalah lingkungan pengembangan web lokal yang dirancang khusus untuk macOS, dengan integrasi PHP, berbagai server web (Caddy & Nginx), database (MySQL, PostgreSQL, MongoDB), layanan cache (Redis, Memcached), serta berbagai alat pengembangan lain. ServBay mempermudah konfigurasi dan manajemen semua paket ini, sehingga menjalankan proyek berbasis PHP di lokal jadi sangat mudah.
Dokumen ini akan membimbing Anda membuat dan menjalankan proyek Zend Framework (Laminas) di lingkungan ServBay, serta membahas integrasi dengan database dan layanan cache yang disediakan ServBay.
Prasyarat
Sebelum memulai, pastikan Anda telah memenuhi hal berikut:
- Instalasi ServBay: ServBay sudah terpasang dan berjalan di macOS Anda. Jika belum, kunjungi Situs Resmi ServBay untuk instruksi unduh dan instalasi.
- Paket yang Dibutuhkan: Pastikan ServBay telah memasang dan menjalankan paket-paket berikut:
- Minimal satu versi PHP (PHP 8.x atau lebih baru disarankan karena Zend Framework/Laminas biasanya membutuhkan versi terbaru PHP).
- Web server (Caddy atau Nginx).
- Composer (biasanya sudah tersedia di ServBay).
- Database (misal MySQL, PostgreSQL) dan layanan cache (misal Memcached, Redis) yang akan Anda gunakan. Semua dapat diaktifkan lewat Control Panel ServBay.
Membuat Proyek Zend Framework
ServBay merekomendasikan agar proyek website Anda disimpan dalam folder /Applications/ServBay/www
. Ini memudahkan ServBay mengelola dan mengonfigurasi website Anda secara otomatis.
Masuk ke Direktori Root Website
Buka Terminal dan arahkan ke root situs yang difavoritkan ServBay:
bashcd /Applications/ServBay/www
1Membuat Proyek dengan Composer
Composer telah terpasang di ServBay, tak perlu instalasi tambahan. Gunakan perintah
create-project
Composer untuk membuat proyek Zend Framework (Laminas skeleton application) baru, misalkan pada folderservbay-zend-app
:bashcomposer create-project laminas/laminas-skeleton-application servbay-zend-app
1Perintah ini otomatis mengunduh skeleton Zend Framework (Laminas) ke direktori
servbay-zend-app
beserta semua dependensinya.Masuk ke Direktori Proyek
Masuk ke folder proyek baru Anda:
bashcd servbay-zend-app
1
Mengonfigurasi Web Server
Agar proyek Zend Framework Anda bisa diakses lewat browser, Anda perlu mengatur sebuah website di ServBay.
- Buka Control Panel ServBay: Jalankan aplikasi ServBay.
- Akses Pengaturan Website: Temukan dan klik tab Websites pada Control Panel ServBay.
- Tambah Website Baru: Klik tombol
+
di kiri bawah untuk menambahkan konfigurasi website baru. - Isi Informasi Website:
- Nama: Pilih nama yang mudah dikenali, misal
My Zend Dev Site
. - Domain: Masukkan domain untuk mengakses proyek ini. Untuk mencegah bentrok dengan domain publik, gunakan akhiran seperti
.local
atau.test
, misalservbay-zend-test.local
. ServBay otomatis akan mengatur DNS lokal. - Tipe Website: Pilih
PHP
. - Versi PHP: Pilih versi PHP yang diinginkan (misal
8.3
). Pastikan versi tersebut telah diinstall dan dijalankan di ServBay. - Dokumen Root (Document Root): Merujuk ke folder yang berisi file utama web server. Entry point Zend Framework (
index.php
) berada di folderpublic
pada proyek Anda. Maka, root dokumen harus di-set ke/Applications/ServBay/www/servbay-zend-app/public
.
- Nama: Pilih nama yang mudah dikenali, misal
- Simpan & Restart: Klik tombol Save. ServBay akan meminta konfirmasi untuk mengaplikasikan perubahan dan otomatis me-reload konfigurasi web server, membuat website baru aktif.
Untuk langkah detail, silakan cek bab Menambahkan Website Pertama di dokumentasi ServBay.
Contoh Dasar "Hello ServBay!"
Sekarang, kita akan modifikasi kode agar saat mengakses root URL (/
) akan muncul pesan "Hello ServBay!".
Konfigurasi Routing & Controller (
module.config.php
)Edit file
module/Application/config/module.config.php
dan pastikan berisi konfigurasi routing serta controller berikut: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 lain ], ], '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 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
49Catatan: Kode di atas adalah bagian konfigurasi dasar dari
module.config.php
. Anda harus menggabungkannya ke array konfigurasi yang sudah ada. Pastikan rute'home'
dan factory untukController\IndexController::class
betul-betul disetting.Buat atau Edit Controller (
IndexController.php
)Buat/ubah file
module/Application/src/Controller/IndexController.php
. PastikanindexAction
me-return ViewModel dengan pesan berikut:php<?php declare(strict_types=1); namespace Application\Controller; use Laminas\Mvc\Controller\AbstractActionController; use Laminas\View\Model\ViewModel; class IndexController extends AbstractActionController { /** * Action default untuk menampilkan halaman selamat datang. */ public function indexAction() { // Mengembalikan ViewModel dan mengoper variabel 'message' ke view return new ViewModel([ 'message' => 'Hello ServBay!', ]); } // ... action tambahan lainnya }
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 Edit File View (
index.phtml
)Buat/edit file
module/Application/view/application/index/index.phtml
agar menampilkan variable pesan dari controller:php<h1><?php echo $this->message; ?></h1>
1Di sini Anda menggunakan view helper Zend Framework (Laminas)
$this->message
untuk mengambil data dari controller.
Mengakses Website
Buka browser Anda dan kunjungi domain yang telah Anda atur di ServBay, misal https://servbay-zend-test.local
.
Jika semua sudah dikonfigurasi dengan semestinya, Anda akan melihat halaman yang menampilkan Hello ServBay!
. Ini artinya proyek Zend Framework Anda berhasil berjalan di ServBay.
Contoh Integrasi Database & Cache
ServBay menyediakan berbagai database dan layanan cache. Bagian berikut mendemokan cara menghubungkan Zend Framework dengan Memcached, Redis, MySQL, dan PostgreSQL.
Catatan Penting: Contoh-contoh database dan cache berikut dibuat berdiri sendiri untuk tujuan demo. Pada aplikasi nyata, Anda bisa memilih satu atau lebih database/cache sesuai kebutuhan yang dikelola dengan dependency injection. Pastikan sebelum menjalankan contoh ini, servis terkait (MySQL, PostgreSQL, Memcached, Redis) sudah Anda aktifkan di ServBay.
Contoh Interaksi Database – Membuat Tabel
Kita mulai dengan contoh interaksi basis data menggunakan komponen Laminas DB, termasuk membuat tabel sederhana secara manual (bukan memakai tools migrations penuh).
Pasang Komponen Laminas DB
Di root proyek, jalankan Composer:
bashcomposer require laminas/laminas-db
1Buat Database Manual
Sebelum mencoba contoh, Anda perlu membuat database bernama
servbay_zend_app
di database pilihan Anda lewat tools seperti phpMyAdmin, pgAdmin, atau MongoDB Compass dari ServBay Control Panel. Username default MySQL/MariaDB di ServBay adalahroot
dengan passwordpassword
, begitu juga PostgreSQL.Script Pembuatan Tabel (
create_users_table.php
)Buat file PHP (misal
create_users_table.php
) di proyek atau folder sementara, lalu isi kode berikut:php<?php // create_users_table.php use Laminas\Db\Adapter\Adapter; use Laminas\Db\Sql\Sql; // Misalkan menggunakan MySQL atau MariaDB $adapter = new Adapter([ 'driver' => 'Pdo_Mysql', // atau 'Pdo_Pgsql' 'database' => 'servbay_zend_app', 'username' => 'root', 'password' => 'password', // password default ServBay 'hostname' => '127.0.0.1', // 'port' => 3306, // port MySQL default // 'port' => 5432, // port PostgreSQL default ]); $sql = new Sql($adapter); // Definisikan SQL untuk membuat 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 "Executing SQL:\n"; echo $sql->buildSqlString($create, $adapter->getPlatform()) . "\n"; try { // Jalankan SQL $adapter->query( $sql->buildSqlString($create, $adapter->getPlatform()), Adapter::QUERY_MODE_EXECUTE ); echo "Tabel 'users' berhasil dibuat.\n"; } catch (\Exception $e) { echo "Error saat 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: Ini hanya contoh eksekusi manual. Di aplikasi nyata, biasanya Anda akan menggunakan tool Laminas Migration untuk manajemen schema database.
Jalankan script via PHP CLI dari terminal:
bashphp create_users_table.php
1
Contoh Integrasi MySQL
Berikut cara menghubungkan dan melakukan query MySQL dari controller Zend Framework.
Konfigurasi Koneksi Database
Edit
config/autoload/global.php
untuk pengaturan koneksi MySQL:php<?php // config/autoload/global.php return [ 'db' => [ 'driver' => 'Pdo_Mysql', 'database' => 'servbay_zend_app', // Pastikan DB sudah dibuat 'username' => 'root', // Username default ServBay 'password' => 'password', // Password default ServBay 'hostname' => '127.0.0.1', 'port' => 3306, // Port MySQL default 'charset' => 'utf8mb4', ], // ... konfigurasi global lainnya ];
1
2
3
4
5
6
7
8
9
10
11
12
13
14Konfigurasi Factory Controller (
module.config.php
)Agar bisa inject
Laminas\Db\Adapter\Adapter
ke controller, atur factory khusus padamodule/Application/config/module.config.php
. Jika sudah adaInvokableFactory
, ganti dengan:php<?php // module/Application/config/module.config.php namespace Application; use Laminas\ServiceManager\Factory\InvokableFactory; // Tetap jika masih digunakan use Laminas\Db\Adapter\AdapterInterface; // Tambahkan return [ // ... konfigurasi lain 'controllers' => [ 'factories' => [ Controller\IndexController::class => function($container) { // Ambil DB adapter dari Service Manager $adapter = $container->get(AdapterInterface::class); // Buat dan kembalikan IndexController, inject adapter return new Controller\IndexController($adapter); }, // Tambah factory controller lain bila perlu ], ], 'service_manager' => [ 'aliases' => [ // Alias AdapterInterface ke Adapter sesungguhnya AdapterInterface::class => 'Laminas\Db\Adapter\Adapter', ], 'factories' => [ // Factory untuk Laminas\Db\Adapter\Adapter '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
27
28
29
30
31
32Catatan: Pastikan seluruh konfigurasi di atas digabungkan ke file Anda (terutama bagian
service_manager
, alias, dan factory-nya).Konfigurasi Routing (
module.config.php
)Tambahkan routing baru untuk MySQL pada
module/Application/config/module.config.php
:php<?php // module/Application/config/module.config.php namespace Application; use Laminas\Router\Http\Literal; // ... use lain return [ 'router' => [ 'routes' => [ // ... routing yang sudah ada '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
34
35Tambahkan pada array
'routes'
.Tambahkan Method Controller (
IndexController.php
)Modifikasi
module/Application/src/Controller/IndexController.php
, tambahkan konstruktor untuk injectAdapter
dan buat methodmysqlAddAction
&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; // Tambahkan use Laminas\Db\Sql\Sql; // Tambahkan class IndexController extends AbstractActionController { private $adapter; // Properti privat adapter // Konstruktor untuk menerima AdapterInterface public function __construct(AdapterInterface $adapter) { $this->adapter = $adapter; } /** * Action default welcome. */ public function indexAction() { return new ViewModel([ 'message' => 'Hello ServBay!', ]); } /** * Action menambah user ke tabel 'users' di MySQL. */ public function mysqlAddAction() { $sql = new Sql($this->adapter); $insert = $sql->insert('users') ->values([ 'name' => 'ServBay Demo User', 'email' => '[email protected]', // Email contoh ]); $statement = $sql->prepareStatementForSqlObject($insert); $result = $statement->execute(); // Cek hasil insert, return pesan sederhana $message = $result->getAffectedRows() > 0 ? 'MySQL User berhasil ditambahkan.' : 'Gagal menambah user MySQL.'; return new ViewModel([ 'message' => $message, ]); } /** * Action mengambil semua user dari tabel 'users' di 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; } // Ubah hasil ke JSON untuk ditampilkan di view return new ViewModel([ 'users' => json_encode($users, JSON_PRETTY_PRINT), ]); } // ... method action 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
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
77
78Tempelkan konstruktor dan dua fungsi ini ke dalam class.
Buat File View
Buat
module/Application/view/application/index/mysql-add.phtml
:php<h1><?php echo $this->message; ?></h1>
1Buat
module/Application/view/application/index/mysql.phtml
:php<h1>MySQL Users</h1> <pre><?php echo $this->users; ?></pre>
1
2Akses Contoh MySQL
Pastikan layanan MySQL ServBay aktif. Kunjungi
https://servbay-zend-test.local/mysql-add
untuk menambah user. Harusnya akan muncul "MySQL User berhasil ditambahkan." Akseshttps://servbay-zend-test.local/mysql
untuk melihat daftar user (tampil dalam format JSON).
Contoh Integrasi PostgreSQL
Cara menghubungkan dan query PostgreSQL pada Proyek Zend Framework:
Konfigurasi Koneksi Database
Edit
config/autoload/global.php
untuk setting PostgreSQL (Catatan: Jika ingin MySQL & PostgreSQL berjalan bersamaan, Anda butuh pengaturan lebih lanjut/config environment terpisah. Contoh ini menimpa ke 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 global lainnya ];
1
2
3
4
5
6
7
8
9
10
11
12
13Konfigurasi Factory Controller (
module.config.php
)(Sama dengan contoh MySQL) Pastikan konfigurasi factory & service_manager sudah benar menginject AdapterInterface ke IndexController.
Konfigurasi Routing (
module.config.php
)Tambahkan routing untuk contoh PostgreSQL:
php<?php // module/Application/config/module.config.php namespace Application; use Laminas\Router\Http\Literal; return [ 'router' => [ 'routes' => [ // ... rute sebelumnya '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
33
34Tambahkan Method Controller (
IndexController.php
)Tambahkan method baru pada controller:
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 yang ada /** * Action menambah user ke tabel 'users' via PostgreSQL. */ public function pgsqlAddAction() { $sql = new Sql($this->adapter); $insert = $sql->insert('users') ->values([ 'name' => 'ServBay Demo User', 'email' => '[email protected]', // Email contoh ]); $statement = $sql->prepareStatementForSqlObject($insert); $result = $statement->execute(); $message = $result->getAffectedRows() > 0 ? 'PostgreSQL User berhasil ditambahkan.' : 'Gagal menambah user PostgreSQL.'; return new ViewModel([ 'message' => $message, ]); } /** * Action mengambil semua user dari tabel 'users' via 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
65Tambahkan kedua method di atas ke dalam class.
Buat File View
Buat
module/Application/view/application/index/pgsql-add.phtml
:php<h1><?php echo $this->message; ?></h1>
1Buat
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 ServBay menyala. Kunjungi
https://servbay-zend-test.local/pgsql-add
untuk menambah user, lalu akseshttps://servbay-zend-test.local/pgsql
untuk melihat data user dalam format JSON.
Contoh Integrasi Memcached
Contoh cache data menggunakan Memcached di controller Zend Framework.
Instalasi Adapter Memcached
Tambahkan pada
composer.json
:json// composer.json { "require": { "laminas/laminas-skeleton-application": "^1.0", "laminas/laminas-cache-storage-adapter-memcached": "^2.0" // Tambah baris ini // ... dependency lain }, // ... konfigurasi lain }
1
2
3
4
5
6
7
8
9Kemudian jalankan update composer:
bashcomposer update
1Ekstensi
memcached
untuk PHP sudah terpasang di ServBay.Konfigurasi Routing (
module.config.php
)Tambahkan rute untuk Memcached:
php<?php // module/Application/config/module.config.php namespace Application; use Laminas\Router\Http\Literal; return [ 'router' => [ 'routes' => [ // ... rute lain 'memcached' => [ 'type' => Literal::class, 'options' => [ 'route' => '/memcached', 'defaults' => [ 'controller' => Controller\IndexController::class, 'action' => 'memcached', ], ], ], ], ], // ... other configurations ];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24Tambahkan Method Controller (
IndexController.php
)Tambahkan action berikut ke controller:
php<?php declare(strict_types=1); namespace Application\Controller; use Laminas\Mvc\Controller\AbstractActionController; use Laminas\View\Model\ViewModel; // ... use lain use Laminas\Cache\StorageFactory; use Laminas\Cache\Storage\StorageInterface; class IndexController extends AbstractActionController { // ... constructor dan action sebelumnya /** * Action contoh cache Memcached. */ public function memcachedAction() { // Buat instance cache Memcached // Memcached default ServBay ada di 127.0.0.1:11211 $cache = StorageFactory::factory([ 'adapter' => [ 'name' => 'memcached', 'options' => [ 'servers' => [ ['127.0.0.1', 11211], ], 'ttl' => 300, // Masa berlaku cache 300 detik ], ], 'plugins' => [ // Plugin helper cache umum 'serializer', 'exception_handler' => ['throw_exceptions' => false], ], ]); $cacheKey = 'my_memcached_data'; $cachedData = $cache->getItem($cacheKey, $success); if (!$success) { // Jika cache miss $cachedData = 'Hello Memcached! (Data dari sumber, dicache pada ' . date('Y-m-d H:i:s') . ')'; $cache->setItem($cacheKey, $cachedData); $cachedData .= ' - CACHE MISS'; } else { // Jika cache hit $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
58Masukkan method di atas ke class.
Buat File View
Buat
module/Application/view/application/index/memcached.phtml
:php<h1>Contoh Memcached</h1> <p><?php echo $this->message; ?></p>
1
2Akses Contoh Memcached
Pastikan servis Memcached ServBay aktif. Kunjungi
https://servbay-zend-test.local/memcached
. Pada akses pertama akan muncul pesan "CACHE MISS". Selanjutnya, dalam waktu 300 detik dari cache, Anda akan melihat "CACHE HIT" dan waktu cache tidak berubah—menandakan data diambil dari cache.
Contoh Integrasi Redis
Contoh penggunaan Redis sebagai caching atau storage pada proyek Zend Framework.
Instalasi Adapter Redis
Tambahkan ke
composer.json
:json// composer.json { "require": { "laminas/laminas-skeleton-application": "^1.0", "laminas/laminas-cache-storage-adapter-redis": "^2.0", // Tambahkan "ext-redis": "*" // Pastikan ekstensi redis PHP terpasang // ... dependency lain }, // ... konfigurasi lain }
1
2
3
4
5
6
7
8
9
10Setelah itu jalankan:
bashcomposer update
1Ekstensi
redis
PHP sudah tersedia di ServBay.Konfigurasi Routing (
module.config.php
)Tambahkan rute untuk Redis:
php<?php // module/Application/config/module.config.php namespace Application; use Laminas\Router\Http\Literal; return [ 'router' => [ 'routes' => [ // ... rute lain 'redis' => [ 'type' => Literal::class, 'options' => [ 'route' => '/redis', 'defaults' => [ 'controller' => Controller\IndexController::class, 'action' => 'redis', ], ], ], ], ], // ... other configurations ];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24Tambahkan Method Controller (
IndexController.php
)Tambahkan method berikut:
php<?php declare(strict_types=1); namespace Application\Controller; use Laminas\Mvc\Controller\AbstractActionController; use Laminas\View\Model\ViewModel; // ... use lain use Laminas\Cache\StorageFactory; use Laminas\Cache\Storage\StorageInterface; class IndexController extends AbstractActionController { // ... constructor dan action sebelumnya /** * Action contoh penggunaan Redis. */ public function redisAction() { // Membuat instance Redis cache // Default Redis ServBay berjalan pada 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' => [ // Plugin cache umum 'serializer', 'exception_handler' => ['throw_exceptions' => false], ], ]); $cacheKey = 'my_redis_data'; $cachedData = $cache->getItem($cacheKey, $success); if (!$success) { $cachedData = 'Hello Redis! (Data dari sumber, dicache 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
56
57
58
59Masukkan ke class controller Anda.
Buat File View
Buat
module/Application/view/application/index/redis.phtml
:php<h1>Contoh Redis</h1> <p><?php echo $this->message; ?></p>
1
2Akses Contoh Redis
Pastikan servis Redis ServBay aktif. Kunjungi
https://servbay-zend-test.local/redis
. Akses pertama akan tampil "CACHE MISS". Selanjutnya–dalam waktu 300 detik–akan muncul "CACHE HIT" dan waktu tidak berubah, menandakan data berasal dari cache.
Kesimpulan
Setelah menjalankan langkah-langkah di atas, Anda telah berhasil membuat, mengonfigurasi, dan menjalankan proyek Zend Framework (Laminas) di lingkungan lokal ServBay. Anda juga sudah mempelajari cara mengatur web server menggunakan fitur websites di ServBay, serta melakukan integrasi dan penggunaan database MySQL, PostgreSQL, serta cache Memcached dan Redis yang disediakan oleh ServBay.
ServBay membuat setup dan manajemen lingkungan pengembangan lokal menjadi lebih sederhana, sehingga Anda dapat fokus pada pengembangan kode dan proyek. Dengan fleksibilitas dan kelengkapan paket-paket perangkat lunaknya, menggunakan ServBay akan membantu Anda meniru lingkungan produksi serta meningkatkan efisiensi pengembangan.