Zend Framework 2.0 by Example: Beginner's Guide

4 (1 reviews total)
By Krishna Shasankar V
    Advance your knowledge in tech with a Packt subscription

  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Getting Started with Zend Framework 2.0

About this book

ZF2 is the latest update to the well-known Zend Framework. This version has considerably eased the process of building complex web applications with minimal development effort using plug and play components. ZF2 also provides a highly robust and scalable framework for developing web applications.

"Zend Framework 2.0 by Example: Beginner’s Guide" will guide you through the process of developing powerful web applications using ZF2. It covers all aspects of Zend Framework application development right from installation and configuration; the tasks are designed in a way that readers can easily understand and use them to build their own applications with ease.

"Zend Framework 2.0 by Example: Beginner’s Guide" begins with the basic installation and configuration of the Zend Framework. As you progress through the exercises, you will become thoroughly acquainted with ZF2. With this book, you will learn about the basic concepts of building solid MVC web applications using Zend Framework 2.0. The detailed step-by-step instructions will enable you to build functionality such as a group chat, a file & media sharing service, search, and a simple store to name a few. You will also use a wide range of external modules to implement features that are not natively available. By the end of the book, you will be well versed in building complex, functionality-rich web applications using Zend Framework 2.0.

"Zend Framework 2.0 by Example: Beginner’s Guide" provides everything that you need for building functionality-rich web applications with simple real world examples and tasks.

Publication date:
July 2013


Chapter 1. Getting Started with Zend Framework 2.0

In this chapter we will get our development environment set up and configured in order to start development with Zend Framework 2.0. We will set up a PHP Application Server, install MySQL, and create a development database that will be used in subsequent chapters for our Zend Framework learning exercises. So, let's get started.


Zend Framework 2.0

The last major release of Zend Framework, which happened in 2007, was version 1.0; during the last five years, Zend Framework has undergone a lot of changes to be a successful PHP-based framework. But by merely updating the framework, Zend Framework has retained some of the issues that were inherently present in Zend Framework 1.0.

Zend Framework 2.0 is an attempt to make Zend Framework better by rearchitecting the framework right from the core. Some of the key features of Zend Framework 2.0 over its previous version are listed as follows:

  • PHP 5.3 features such as namespaces and closures

  • A modular application architecture

  • Event manager

  • Dependency Injection (DI)

We will get to know about implementing the new features of Zend Framework 2.0 in the coming chapters.

In this chapter we will cover the installation and configuration of some of the prerequisites of Zend Framework 2.0. ZF2 can be installed on most PHP-enabled web servers that support PHP 5.3.3 or later.

We have used Zend Server Community Edition as our default web server; however, any other PHP stack that supports PHP 5.3.3 can be used. Alternatively, you can also download Apache and PHP separately and install PHP over Apache.


To simplify the installation process, I am using Linux as the primary development environment in this book. All the tools used in this book are available for Windows and can be used to perform the same activity.


Introduction to Zend Server Community Edition (CE)

Zend Server Community Edition is the free version of the popular Zend Server stack. The Zend Server stack provides a pre-integrated PHP application stack that could be used across development, testing, and production. This enables application development teams to have a consistent environment across all stages of development.

Zend Server CE also provides features such as Zend Optimizer+ for PHP bytecode caching and Zend Guard for encoding files.

Zend Server CE – system requirements

Zend Server offers installers for Windows, Mac OS X, and a universal installation package compatible with most Linux distributions.

More details on the installation requirements can be found at http://www.zend.com/en/products/server/system-requirements.


Time for action – installing Zend Server CE

Our next step will be to download and install Zend Server CE; I am running Ubuntu 12.04 Precise Pangolin. The installation procedure for other operating systems could be different; you can always refer to the Zend Server website for installation instructions. The following are the steps to install the Zend Server CE:

  1. Visit the Zend Server Community Edition website (http://www.zend.com/en/community/zend-server-ce) and download the latest version of Zend Server that is applicable to your operating system. In this case, we will be downloading the Linux installer.

  2. Once the installer is downloaded, extract the contents of the installer to a temporary location:

    $ tar -zxvf ZendServer-5.6.0-RepositoryInstaller-linux.tar.gz
  3. After extracting, the installer needs to be started with administrator privileges:

    $ cd ZendServer-RepositoryInstaller-linux/
    $ sudo ./install_zs.sh 5.3 ce


    We are passing two parameters to the installer. The first one is the version of PHP that needs to be installed; in this case it is 5.3. The second parameter identifies the edition of Zend Server that needs to be installed; in this case it is ce for Community Edition.

  4. During the installation, the installer will request you to download various packages:

  5. Zend Server will be installed into /usr/local/zend by default; the default document root will point to /var/www. You can use the following files to make configuration changes to the Zend Server instance:

    • Apache master configuration is available in /etc/apache2/apache2.conf

    • PHP configuration is controlled by /var/local/zend/etc/php.ini

    The following screenshot shows the installed location of Zend Server:

  6. Once the installation is completed, you should be able to open http://localhost on your web browser. This should take you to a test page like the one shown in the following screenshot:


To restart Zend Server, use the $ sudo service zend-server restart command.

What just happened?

Zend Server CE is installed and ready to be used. Now we have a web server and a compatible version of PHP running—this satisfies the core requirements for running Zend Framework 2.0.

Have a go hero

We will be using Git to check out Zend Framework from Github; one of the major changes that happened to Zend Framework 2.0 is that the source control has changed from SVN to Git.

Your next task will be to install Git. We will be making use of Git when we are setting up our Zend Framework project.


Git binaries can either be downloaded from http://www.git-scm.com/ or installed from your operating system's repositories.

Installation instructions for Git can be found at the following link:


Configuring Zend Server CE

Our next step will be to set up Zend Server CE and make some configuration changes that will enable us to run other PHP applications.

Zend Server CE – Administration Interface

Zend Server CE's Administration Interface is a web-based user interface that provides the following features:

  • Managing PHP extensions

  • Configuring PHP directives

  • Managing Zend Server components

  • Monitoring PHP status, extension status, and application/server logs

In our next task, we will be making a configuration change to Zend Server by using its Administration Interface.


Time for action – configuring Zend Server CE

The Zend Server needs to be configured after the installation is completed. The following are the steps for configuring Zend Server CE:

  1. Open the admin console of Zend Server in your default browser (http://localhost:10081/).


    The Zend Server UI console runs on port 10081 while the web server runs on port 80. This is why we need to implicitly specify the port number in the URL for accessing the UI console.

  2. When opening the Zend Server Administration Interface for the first time, you will be presented with a configuration wizard. Review and accept the terms and conditions of Zend's End User License Agreement page:

  3. As shown in the following screenshot, you will be asked to set the password for the Zend Server installation:

  4. After the initial configuration wizard is completed, you will be redirected to the Zend Server Administration Interface's home page.

  5. We need to set the session save path. In order to do this, perform the following steps:

    1. Navigate to Directives in Server Setup.

    2. Search for session.save_path.

    3. Set the value to /tmp.

    4. Click on Save Changes and then Restart PHP.

What just happened?

We have successfully modified a server configuration using Zend Server's Administration Interface and we have restarted the PHP instance running on Zend Server.



MySQL doesn't need an introduction—it is the world's most widely used open source database application. It's free and is available on the Internet to individuals and businesses that wish to develop their websites and applications using the MySQL database.

Zend Framework 2.0 has driver support for MySQL along with SQLite, PostgreSQL, and Microsoft SQL Server.

Our next exercise will be to install MySQL on our development machine. MySQL is available for download from all Linux repositories. Windows and Mac users will have to download the installer from the MySQL website (http://dev.mysql.com/downloads/).


Windows and Mac users can skip this section if they have chosen to install MySQL Server as a part of their Zend Server CE installation. The Zend Server installer allows Windows and Mac users to download and install MySQL Server as a part of the installation.


Time for action – installing MySQL

MySQL Server and Client need to be installed using the following steps; we will be using MySQL as our primary database in this book:

  1. In a standard Ubuntu installation, MySQL can be installed by executing the following command in the shell prompt:

    $ sudo apt-get install mysql-server mysql-client
  2. After the installation is complete, MySQL Server will start automatically. To check if MySQL Server is running, run the following command:

    $ sudo netstat -tap | grep mysql
  3. The command should give an output that is similar to the following; this means that the MySQL daemon is running:

    tcp     0      0 localhost:mysql     *:*     LISTEN      923/mysqld
  4. If, for some reason, MySQL Server is not running, you can start the server by running the restart command:

    $ sudo service mysql restart

What just happened?

We have just installed MySQL; we have the LAMP stack ready too. Our next step will be to create a database in MySQL Server.


Since we are using Zend Server, we don't need to install the php5-mysql package. If you are using a stack that doesn't have MySQL support enabled by default, you will have to install the necessary packages manually.

Have a go hero

Having gone through this section, feel free to attempt the task in the following section.


phpMyAdmin is a free, open source web-based database administration tool written in PHP. phpMyAdmin provides a web-based UI to manage MySQL Database Server; add / remove / manage databases, users, privileges; and so on. In this book, we will be using phpMyAdmin as the database Administration Interface for managing our database(s).

Now that we have Apache, PHP, and MySQL installed, our next step will be to create a blank database in MySQL Server.

For doing this, we need to install and configure phpMyAdmin in the Zend Server.


phpMyAdmin can either be downloaded from http://www.phpmyadmin.net/ or installed from your operating system's repositories.

Installation instructions for phpMyAdmin can be found at the following link:


In our next task we will be creating a MySQL database, creating users in the MySQL server and also grant them access permissions to connect to the database and perform database operations.


Time for action – creating a database

To create a new database, open an instance of phpMyAdmin in your web browser and follow the steps described here:

  1. Open phpMyAdmin in your web browser by visiting http://localhost/phpmyadmin:

  2. Choose Databases, enter the name of the new database as zf_app in Create new database, and click on Create:

  3. After creating the database, create a database user for this database; this can be done by selecting Add a new user from Privileges. Provide the following details:

    User field


    User name






    After doing this you will get the following screen:

  4. After the user is created, go to the Privileges section and choose Edit Privileges for the zf_user.

  5. In the Database-specific privileges section, select the zf_app database.

  6. You will be redirected to the privileges section of the zf_app database for the zf_user user. Choose Check All and click on Go.

You can now test the database by logging out of phpMyAdmin and logging in again with the user credentials of zf_user. You should now be able to see only the zf_app database.

What just happened?

We just created our first database in MySQL. We have also created a user in the database and mapped the user to the database with administrative rights; we can now use these credentials in the application that we will be building in our next chapters.

Have a go hero

Now that you have the PHP web server up and running and also have a MySQL database, create a simple table called Students and add a few records to the table using phpMyAdmin.

Your task will be to create a simple PHP web page that will display all the records in the Students table in the page.

Pop quiz – Zend Framework 2.0

Q1. What is the minimum version of PHP needed to run Zend Framework 2.0?

  1. PHP 4.3 and above

  2. PHP 5.2.0 and above

  3. PHP 5.3.3 and above

  4. PHP 5.4.7 and above

Q2. What is the default location of php.ini in the new Zend Server installation?

  1. /home/<user>/etc/php/php.inc

  2. /etc/php/php.ini

  3. /var/www/php.ini

  4. /usr/local/zend/etc/php.ini



In this chapter we have learned the setup and configuration of Zend Server's PHP application stack. We went on to install MySQL Server and created our first database. In your exercises, you have learned about the installation of Git and phpMyAdmin.

In the next chapter, we will learn about the structure of a Zend Framework project and core MVC components such as views and controllers.

About the Author

  • Krishna Shasankar V

    Krishna Shasankar V is a web developer with 7 years of extensive development experience in PHP. He leads a team of engineers at Lister Technologies developing enterprise class retail and e-commerce solutions. He is a Zend Certified Engineer in PHP 5 and Zend Framework. He also has a Bachelor's degree in Information Technology from Anna University, Chennai, and a Master's degree in Software Systems from Birla Institute of Technology and Science, Pilani. In his spare time, he enjoys music, photography, and travel (especially when combined). You can contact Krishna and leave some comments on his blog (www.clickoffline.com).

    Browse publications by this author

Latest Reviews

(1 reviews total)
I haven't finished reading this book yet.
Zend Framework 2.0 by Example: Beginner's Guide
Unlock this book and the full library for FREE
Start free trial