Create and Run a Zend Framework Project
What is Zend Framework?
Zend Framework is an open-source PHP framework providing a set of object-oriented libraries for building modern web applications and services. Known for its modularity and high performance, it is suitable for building applications ranging from small to large enterprise-level systems.
Key Features and Advantages of Zend Framework
- Modular Design: Zend Framework uses a modular design, allowing developers to choose and use components as needed.
- High Performance: With an optimized architecture and caching mechanisms, Zend Framework delivers excellent performance.
- Flexibility: It can integrate various third-party libraries and extensions, suitable for projects of all sizes.
- Community Support: It has a large developer community and a rich ecosystem.
- Good Documentation: It provides comprehensive documentation and tutorials to help developers get started quickly.
Zend Framework is suitable for building high-quality web applications and APIs, suitable for projects ranging from small apps to large enterprise systems.
Create and Run a Zend Framework Project Using ServBay
In this article, we will use ServBay's PHP environment to create and run a Zend Framework project. We will utilize ServBay's 'Host' feature to set up a web server and achieve project access through simple configuration.
Note: If You Were a NGINX or Apache User
ServBay uses Caddy as the default web server. For users migrating from NGINX and Apache to ServBay, there are some key differences to note:
Caddy Configuration
ServBay has Caddy built-in and optimized by default. Developers only need to use ServBay's 'Host' feature to manage sites without manually modifying Caddy configuration files.
Rewrite Rules and .htaccess
In NGINX and Apache, developers usually have to write their own Rewrite rules and .htaccess files to handle URL rewriting and other configurations. However, ServBay comes pre-configured with Caddy rules, so unless there's a special need, developers don't need to write these rules themselves.
Learn More
For more information, see Rewrite and .htaccess, How to Migrate an Apache Site to ServBay, How to Migrate a NGINX Site to ServBay.
Create a Zend Framework Project
TIP
ServBay recommends developers place websites in the /Applications/ServBay/www
directory for easy management.
Install Composer
ServBay comes with Composer pre-installed, so there's no need to install it separately.
Create a Zend Framework Project
Use Composer to create a new Zend Framework project:
bashcd /Applications/ServBay/www mkdir servbay-zend-app cd servbay-zend-app composer create-project zendframework/skeleton-application .
1
2
3
4Enter the Project Directory
Enter the newly created Zend Framework project directory:
bashcd /Applications/ServBay/www/servbay-zend-app
1
Initialize Configuration
Configure Environment Variables
Configure database connection information and other environment variables in the
config/autoload/global.php
file. Ensure the following settings are correctly configured:phpreturn [ 'db' => [ 'driver' => 'Pdo_Mysql', 'database' => 'servbay_zend_app', 'username' => 'root', 'password' => 'password', 'hostname' => '127.0.0.1', ], ];
1
2
3
4
5
6
7
8
9
Configure the Web Server
Use ServBay's 'Host' feature to access the Zend Framework project through the web server. In ServBay's 'Host' settings, add a new host:
- Name:
My First Zend Dev Site
- Domain:
servbay-zend-test.local
- Website Type:
PHP
- PHP Version: Select
8.3
- Website Root Directory:
/Applications/ServBay/www/servbay-zend-app/public
For detailed setup steps, refer to Adding Your First Website.
Add Example Code
Add the following code to the module/Application/config/module.config.php
file to output "Hello ServBay!":
return [
'router' => [
'routes' => [
'home' => [
'type' => 'Literal',
'options' => [
'route' => '/',
'defaults' => [
'controller' => 'Application\Controller\Index',
'action' => 'index',
],
],
],
],
],
'controllers' => [
'factories' => [
'Application\Controller\Index' => InvokableFactory::class,
],
],
'view_manager' => [
'template_path_stack' => [
__DIR__ . '/../view',
],
],
];
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
Add the following code to the module/Application/src/Controller/IndexController.php
file:
namespace Application\Controller;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
public function indexAction()
{
return new ViewModel([
'message' => 'Hello ServBay!',
]);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
Add the following code to the module/Application/view/application/index/index.phtml
file:
<?php echo $this->message; ?>
Access the Website
Open your browser and visit https://servbay-zend-test.local
, you will see the page output Hello ServBay!
.
NoSQL Database Example
Memcached Example
Install Memcached Extension
In ServBay, the Memcached extension is pre-installed, so there is no need to install it separately.
Configure Memcached
Add the Memcached dependency in the
composer.json
file:json{ "require": { "laminas/laminas-cache-storage-adapter-memcached": "^2.0" } }
1
2
3
4
5Then run
composer update
to install the dependency.Configure Route
Add the following code in the
module/Application/config/module.config.php
file:phpreturn [ 'router' => [ 'routes' => [ 'memcached' => [ 'type' => 'Literal', 'options' => [ 'route' => '/memcached', 'defaults' => [ 'controller' => 'Application\Controller\Index', 'action' => 'memcached', ], ], ], ], ], ];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16Use Memcached
Use cache in the controller:
phpnamespace Application\Controller; use Laminas\Mvc\Controller\AbstractActionController; use Laminas\View\Model\ViewModel; use Laminas\Cache\StorageFactory; class IndexController extends AbstractActionController { public function memcachedAction() { $cache = StorageFactory::factory([ 'adapter' => [ 'name' => 'memcached', 'options' => [ 'servers' => [ ['127.0.0.1', 11211], ], ], ], ]); $cacheKey = 'my_cache_key'; $cachedData = $cache->getItem($cacheKey, $success); if (!$success) { $cachedData = 'Hello Memcached!'; $cache->setItem($cacheKey, $cachedData); } 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
34Add the following code to the
module/Application/view/application/index/memcached.phtml
file:php<?php echo $this->message; ?>
1Open your browser and visit
https://servbay-zend-test.local/memcached
.
Redis Example
Install Redis Extension
In ServBay, the Redis extension is pre-installed, so there is no need to install it separately.
Configure Redis
Add the Redis dependency in the
composer.json
file:json{ "require": { "laminas/laminas-cache-storage-adapter-redis": "^2.0" } }
1
2
3
4
5Then run
composer update
to install the dependency.Configure Route
Add the following code in the
module/Application/config/module.config.php
file:phpreturn [ 'router' => [ 'routes' => [ 'redis' => [ 'type' => 'Literal', 'options' => [ 'route' => '/redis', 'defaults' => [ 'controller' => 'Application\Controller\Index', 'action' => 'redis', ], ], ], ], ], ];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16Use Redis
Use cache in the controller:
phpnamespace Application\Controller; use Laminas\Mvc\Controller\AbstractActionController; use Laminas\View\Model\ViewModel; use Laminas\Cache\StorageFactory; class IndexController extends AbstractActionController { public function redisAction() { $cache = StorageFactory::factory([ 'adapter' => [ 'name' => 'redis', 'options' => [ 'server' => [ 'host' => '127.0.0.1', 'port' => 6379, ], ], ], ]); $cacheKey = 'my_cache_key'; $cachedData = $cache->getItem($cacheKey, $success); if (!$success) { $cachedData = 'Hello Redis!'; $cache->setItem($cacheKey, $cachedData); } 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
35Add the following code to the
module/Application/view/application/index/redis.phtml
file:php<?php echo $this->message; ?>
1Open your browser and visit
https://servbay-zend-test.local/redis
.
Relational Database Example
Create Database Structure and Migration Files
Create Migration Files
Use Laminas's Migrations tool to create migration files:
bashcomposer require laminas/laminas-db
1Edit Migration Files
Create a new migration file in the
data/migrations
directory and edit it to define the database table structure:phpuse Laminas\Db\Adapter\Adapter; use Laminas\Db\Sql\Sql; class CreateUsersTable { public function up(Adapter $adapter) { $sql = new Sql($adapter); $create = $sql->createTable('users') ->addColumn('id', 'integer', ['auto_increment' => true]) ->addColumn('name', 'varchar', ['length' => 255]) ->addColumn('email', 'varchar', ['length' => 255, 'unique' => true]) ->addPrimaryKey('id'); $adapter->query( $sql->buildSqlString($create), Adapter::QUERY_MODE_EXECUTE ); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20Run Migration
Manually run the migration to create the database table:
php$adapter = new Adapter([ 'driver' => 'Pdo_Mysql', 'database' => 'servbay_zend_app', 'username' => 'root', 'password' => 'password', 'hostname' => '127.0.0.1', ]); $migration = new CreateUsersTable(); $migration->up($adapter);
1
2
3
4
5
6
7
8
9
10
MySQL Example
Configure MySQL
Configure MySQL connection information in the
config/autoload/global.php
file:phpreturn [ 'db' => [ 'driver' => 'Pdo_Mysql', 'database' => 'servbay_zend_app', 'username' => 'root', 'password' => 'password', 'hostname' => '127.0.0.1', ], ];
1
2
3
4
5
6
7
8
9Configure Route
Add the following code in the
module/Application/config/module.config.php
file:phpreturn [ 'router' => [ 'routes' => [ 'mysql-add' => [ 'type' => 'Literal', 'options' => [ 'route' => '/mysql-add', 'defaults' => [ 'controller' => 'Application\Controller\Index', 'action' => 'mysqlAdd', ], ], ], 'mysql' => [ 'type' => 'Literal', 'options' => [ 'route' => '/mysql', 'defaults' => [ 'controller' => 'Application\Controller\Index', '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
26Insert User Data
Insert user data in the controller:
phpnamespace Application\Controller; use Laminas\Mvc\Controller\AbstractActionController; use Laminas\View\Model\ViewModel; use Laminas\Db\Adapter\Adapter; use Laminas\Db\Sql\Sql; class IndexController extends AbstractActionController { protected $adapter; public function __construct(Adapter $adapter) { $this->adapter = $adapter; } public function mysqlAddAction() { $sql = new Sql($this->adapter); $insert = $sql->insert('users') ->values([ 'name' => 'ServBay', 'email' => '[email protected]', ]); $this->adapter->query( $sql->buildSqlString($insert), Adapter::QUERY_MODE_EXECUTE ); return new ViewModel([ 'message' => 'User added', ]); } public function mysqlAction() { $sql = new Sql($this->adapter); $select = $sql->select('users'); $result = $this->adapter->query( $sql->buildSqlString($select), Adapter::QUERY_MODE_EXECUTE ); $users = []; foreach ($result as $row) { $users[] = $row; } return new ViewModel([ 'users' => json_encode($users), ]); } }
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
55Add the following code to the
module/Application/view/application/index/mysql-add.phtml
file:php<?php echo $this->message; ?>
1Add the following code to the
module/Application/view/application/index/mysql.phtml
file:php<?php echo $this->users; ?>
1Open your browser and visit
https://servbay-zend-test.local/mysql-add
andhttps://servbay-zend-test.local/mysql
.
PostgreSQL Example
Configure PostgreSQL
Configure PostgreSQL connection information in the
config/autoload/global.php
file:phpreturn [ 'db' => [ 'driver' => 'Pdo_Pgsql', 'database' => 'servbay_zend_app', 'username' => 'root', 'password' => 'password', 'hostname' => '127.0.0.1', ], ];
1
2
3
4
5
6
7
8
9Configure Route
Add the following code in the
module/Application/config/module.config.php
file:phpreturn [ 'router' => [ 'routes' => [ 'pgsql-add' => [ 'type' => 'Literal', 'options' => [ 'route' => '/pgsql-add', 'defaults' => [ 'controller' => 'Application\Controller\Index', 'action' => 'pgsqlAdd', ], ], ], 'pgsql' => [ 'type' => 'Literal', 'options' => [ 'route' => '/pgsql', 'defaults' => [ 'controller' => 'Application\Controller\Index', '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
26Insert User Data
Insert user data in the controller:
phpnamespace Application\Controller; use Laminas\Mvc\Controller\AbstractActionController; use Laminas\View\Model\ViewModel; use Laminas\Db\Adapter\Adapter; use Laminas\Db\Sql\Sql; class IndexController extends AbstractActionController { protected $adapter; public function __construct(Adapter $adapter) { $this->adapter = $adapter; } public function pgsqlAddAction() { $sql = new Sql($this->adapter); $insert = $sql->insert('users') ->values([ 'name' => 'ServBay', 'email' => '[email protected]', ]); $this->adapter->query( $sql->buildSqlString($insert), Adapter::QUERY_MODE_EXECUTE ); return new ViewModel([ 'message' => 'User added', ]); } public function pgsqlAction() { $sql = new Sql($this->adapter); $select = $sql->select('users'); $result = $this->adapter->query( $sql->buildSqlString($select), Adapter::QUERY_MODE_EXECUTE ); $users = []; foreach ($result as $row) { $users[] = $row; } return new ViewModel([ 'users' => json_encode($users), ]); } }
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
55Add the following code to the
module/Application/view/application/index/pgsql-add.phtml
file:php<?php echo $this->message; ?>
1Add the following code to the
module/Application/view/application/index/pgsql.phtml
file:php<?php echo $this->users; ?>
1Open your browser and visit
https://servbay-zend-test.local/pgsql-add
andhttps://servbay-zend-test.localpgsql
.
By following the steps above, you have successfully created and run a Zend Framework project, managed and accessed your project using ServBay's features, and connected to various databases and called data.