Create Zend Framework 2 project framework from zero

Source: Internet
Author: User
Tags autoload file copy stack trace git client zend zend framework

System Requirements : php5.3.3 above version
Project directory:/VAR/WEB/NEWZF2

preparatory work :
Download the Zftool.phar and Zftool modules. To/var/web
Zftool.phar Address: Https://
Address of the Zftool module:


Zftool.phar is a separate file that can be executed php5.3.3 the environment, and the ZF2 framework is built.
Let's see if it works, show the Zftool version number
/var/web under Execution:

[/var/web]$ php Zftool.phar version
zftool-zend Framework 2 command line Tool The
Zftool is using Zend framework 2.2.4

Create a ZF2 project framework

PHP Zftool.phar Create Project <path>.
To execute under/var/web:
PHP Zftool.phar Create Project Newzf2

The results are as follows:

[/var/web]# PHP zftool.phar  Create project Newzf2
ZF2 skeleton application in installed.
In order to execute the skeleton application your need to install the ZF2 library.
Execute: "Composer.phar Install" in NEWZF2 for more
info in newzf2/

Enter/VAR/WEB/NEWZF2 and look at the resulting directory structure.

-rw-r--r--1  composer.json
-rwxr-xr-x 1  composer.phar
drwxr-xr-x 3  config
drwxr-xr-x 3  data
-rw-r--r--1  init_autoloader.php
-rw-r--r--1  LICENSE.txt
drwxr-xr-x 3  Module
drwxr-xr-x 6  public
drwxr-xr-x 3  Vendor

Install the Zend Framework 2 library

Installing the ZF2 class library via Composer.phar


1. Update composer

PHP Composer.phar self-update

2. Install the ZF2 class library

PHP Composer.phar Install

This requires the system to install the GIT client first. Otherwise, you will be prompted, you can not find the git command, the installation is done.

After the installation was successful, the results are as follows:

[/VAR/WEB/NEWZF2] #php Composer.phar install Loading composer repositories with package information installing Dependencies (including Require-dev)-Installing Zendframework/zendframework (2.2.5) Loading from Cache Zendframew
Ork/zendframework suggests installing doctrine/annotations (doctrine annotations >=1.0 for annotation features) Zendframework/zendframework suggests installing ircmaxell/random-lib (fallback random byte generator for Zend\Math\Rand If Openssl/mcrypt extensions are unavailable) Zendframework/zendframework suggests installing Ocramius/proxy-manager ( Proxymanager to handle lazy initialization of services) Zendframework/zendframework suggests installing zendframework/ Zendpdf (zendpdf for creating PDF representations of barcodes) Zendframework/zendframework suggests installing Zendframew Ork/zendservice-recaptcha (Zendservice\recaptcha for rendering Recaptchas in Zend\captcha and/or Zend\form) Writing lock File Generating autoload files

Path to install:/var/web/newzf2/vendor/zendframework/zendframework

Installing the ZF2 class library with Composer.phar is a good way to solve the problem of dependency rather than Zftool.phar

Project Framework Build complete

Access can be achieved by configuring the virtual machine to/var/web/newzf2/public. The default is to access the application module

To create a new modular module

Use Zftool.phar to create the modules that we need (module)


PHP Zftool.phar Create Module <name> [<path>]
<name>              modules (module) name to create
<path>              ZF2 Application root directory (optional)
The results are as follows:
[/var/web]# PHP Zftool.phar Create module user/var/web/newzf2/The
module user has been created IN/VAR/WEB/NEWZF2

The current/var/web/newzf2/module/directory has:


To view the contents of/var/web/newzf2/config/application.config.php:

 * Configuration file generated by Zftool
 * "Previous Configuration file is stored in Applicati On.config.old
 * @see
/return Array (
    ' modules ' = > Array (
        ' application ',
        ' User '
    ' module_listener_options ' => array (
        ' Module_ Paths ' => Array (
            './module ',
            './vendor '
        ' config_glob_paths ' => Array (' Config/autoload /{,*.} {global,local}.php ')

The User module field was added.

Enrich the contents of module, add controller etc.

In order to create the content within the module, you need to Zftool.phar file copy or MV to the project folder/var/web/newzf2, this is to create convenience such as Controller and CONFIG.

Create Controller Controller


1 PHP Zftool.phar Create controller <name> <module>
3 <name> Controller (Controller) name to create
4 (module) name of the <module> controller (Controller)

Execute command under/VAR/WEB/NEWZF2: PHP zftool.phar Create controller Index User

The results are as follows:

[/var/web/newzf2]# PHP Zftool.phar Create controller Index User PHP Fatal error:uncaught exception ' Zend\modulemanager\ Exception\runtimeexception ' with message ' Module (Zftool) could is not initialized. ' in/var/web/newzf2/vendor/ zendframework/zendframework/library/zend/modulemanager/modulemanager.php:175 Stack Trace: #0/var/web/newzf2/ vendor/zendframework/zendframework/library/zend/modulemanager/modulemanager.php (149): Zend\ModuleManager\ Modulemanager->loadmodulebyname (Object (zend\modulemanager\moduleevent)) #1/var/web/newzf2/vendor/ Zendframework/zendframework/library/zend/modulemanager/modulemanager.php (m): zend\modulemanager\modulemanager- >loadmodule (' Zftool ') #2 [internal function]: Zend\modulemanager\modulemanager->onloadmodules (Object (zend\ modulemanager\moduleevent)) #3/var/web/newzf2/vendor/zendframework/zendframework/library/zend/eventmanager/ eventmanager.php (468): Call_user_func (Array, Object (zend\modulemanager\moduleevent)) #4/var/web/newzf2/vendor/ Zendframework/zendframework/library/zend/eventmanager/eventmanager.php (207): zend\eventmanager\eventmanager-> Triggerlisteners (in/var/web/newzf2/vendor/zendframework/zendframework/library/zend/modulemanager/ modulemanager.php on line 175

The reason for the failure is that the Zftool module (modules) was not found and initialized.

Next, let's talk about how to fix this error. installing the Zftool into Modules (module)

Remember the Zftool module you started downloading and now you're going to use it. Copy the entire Zftool directory to the vendor subdirectory in the directory where the project is located, after which the catalog will suffice:

Unzip the Zftool to the/var/web/newzf2/vendor directory.

As follows:

-rw-r--r--1  autoload.php
drwxr-xr-x 2  bin
drwxr-xr-x 2  composer
drwxr-xr-x 3  zendframework
drwxr-xr-x 2  ZF2
drwxr-xr-x 8  Zftool

So Zftool has been installed as a module.

Create controller again succeeded

[/var/web/newzf2]# PHP Zftool.phar Create controller index user The
controller index has been in module user.
The needs to be explained that although controller has been generated, it is still not available because the user/config/module.config.php configuration file, which has an empty array, needs to modify the join rule:
<?php return Array (' Controllers ' => array (' Invokables ' => Array (' User\controller\index
            ' => ' User\controller\indexcontroller ',), ' router ' => array (' Routes ' => array (
                    ' User ' => array (' type ' => ' Literal ', ' Options ' => array (
                    The change is to something specific to your module ' route ' => '/user ',
                        ' Defaults ' => array (//change this value to reflect the namespace in which
                        The controllers for your module are found ' __namespace__ ' => ' User\controller ',
                    ' Controller ' => ' index ', ' action ' => ' index ',
                    ), ' May_terminate ' => true, ' child_routes ' => Array ( This routE is a sane default when developing a module; As you solidify the routes for your module, however,//I/may want to remove it and replace it WI
                    th more//specific routes.
                            ' Default ' => array (' type ' => ' Segment ', ' Options ' => array ( ' Route ' => '/[:controller[/:action]] ', ' Constraints ' => ar      Ray (' controller ' => ' [a-za-z][a-za-z0-9_-]* ', ' action ')
                            => ' [a-za-z][a-za-z0-9_-]* ',], ' defaults ' => array ( ),
        ) ,), ' View_manager ' => array (' Template_path_stack ' => array (' user ' => __dir__.) '/..
   /view ',     ),

other commands of the Zftool

After introducing some of Zftool's most important commands, let's talk about a few other commands.

1, display the calling module (modular)


1 PHP zftool.phar modules [list] shows the calling module (modules)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.