ZF2 (Zend Framework 2) is an open source, object oriented Framework designed for building secure, reliable, high performance and robust web applications and services using PHP 5.3 and above. ZF2 is a progression of Zend Framework 1, a very successful PHP framework. However, it is not backward compatible with ZF1 which requires PHP 5.2. ZF2 uses most of the new features of PHP 5.3, namely namespaces, late static binding, among others.
This blog aims to shed light on the basic setup of ZF2 Framework with a skeleton application and ZFTool. The assumption is that you already have a Apache web server and Php 5.3 or above.
Lets get started:
Open Terminal or ctrl+alt+t
cd /var/www/
Now, there are two ways of making this setup:
1) Directly with Composer or
2) Download the skeleton application and resolve dependency using Composer.
Step 1:
We can use Composer to create a new application from scratch with the Zend Framework. Composer is a tool which helps to manage libraries on a per-project basis. If you do not have Composer, get it from http://getcomposer.org.
Now, follow the below command:
sudo php composer.phar create-project --stability="dev" zendframework/skeleton-application /var/www/zf2-demo
The above set of commands will create a ZF2 application with name “zf2-demo” in the folder /var/www/.
Step 2:
Alternatively, we can use github to install ZendSkeletonApplication.
Go to
https://github.com/zendframework/ZendSkeletonApplication
and click the “Zip” button. (ZendSkeletonApplication-master.zip.)
Extract the zip. Move the extracted file.
mv ZendSkeletonApplication-master /var/www/zf2-demo
Now, we have a skeleton application ready. We still need to resolve some of the library dependency as Zend Skeleton Application is setup to use Composer (http://getcomposer.org) to resolve its dependencies. In application folder, first take the self-update and then install .
Follow the commands below.
cd /var/www/zf2-demo
sudo php composer.phar self-update
sudo php composer.phar install
Now that your project is setup, add controllers and views to start developing your application.
Open the URL to access your Zf2 project.
http://localhost/zf2-demo/public/
You are now ready to code!
ZFTool Setup & Usage:
After the ZF2 setup is done, you would want to add modules and controllers into the
application. The ZFTool comes in real handy in this situation. ZFTool is nothing but a module which can be used to maintain any ZF2 Application. In other words, ZFTool is a utility module which can be used for ZF2 project maintenance.
It runs from the command line and can be installed as ZF2 module or PHAR.
This tool comes with following features:
1) Listing of loaded modules.
2) Create a new project (install the ZF2 skeleton application).
3) Create a new module in exiting Application.
4) Create a new controller in module.
5) Create a new action in a controller.
6) Class-map generator.
5) Install the ZF2 library choosing a specific version.
To install the ZFTool, use one of the following methods:
Using composer
Open console
Go to your application’s directory
/var/www/zf2-demo
Run
sudo php composer.phar require zendframework/zftool:dev-master
zf.php will be installed in the vendor/bin folder. Run it with
sudo php vendor/bin/zf.php
Manual setup:
Clone using
git clone https://github.com/zendframework/ZFTool
or download
https://github.com/zendframework/ZFTool/zipball/master
Extract to
vendor/ZFTool of your project.
Run
sudo php vendor/ZFTool/zf.php
Usage :
zf.php modules - Show loaded modules
zf.php version OR zf.php –version – Shows Zf2 version
zf.php create module <modulename> - Creates new module
The screenshot above shows command for creating a module named User.
zf.php create controller <controllername> <modulename>
The screenshot above shows command for creating User Controller inside module User.
zf.php create action <actionname> <controllername> <modulename>
This screenshot shows command for adding action Add inside User Controller in User module.
References:
http://framework.zend.com/manual/current/en/user-guide/skeleton-application.html
http://framework.zend.com/manual/current/en/modules/zendtool.introduction.html