系统要求:php5.3.3以上版本
项目目录:/var/web/newzf2

准备工作
下载zftool.phar和ZFTOOL模块。到/var/web
zftool.phar地址:https://packages.zendframework.com/zftool.phar
ZFTOOL模块的地址:https://github.com/zendframework/ZFTool/zipball/master

参见http://framework.zend.com/manual/2.2/en/modules/zendtool.introduction.html

zftool.phar 是一个独立的文件,可以再php5.3.3环境下命令行执行,zf2的框架的搭建。
先来看看是否可以正常运行,显示一下 zftool 的版本号
/var/web下执行:

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

创建zf2项目框架

语法:
php zftool.phar create project <path> 。
在/var/web下执行:
php zftool.phar  create project newzf2

结果如下:

[/var/web]# php zftool.phar  create project newzf2
ZF2 skeleton application installed in newzf2.
In order to execute the skeleton application you need to install the ZF2 library.
Execute: "composer.phar install" in newzf2
For more info in newzf2/README.md

进入/var/web/newzf2看一下生成的目录结构。

-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
-rw-r--r-- 1  README.md
drwxr-xr-x 3  vendor

 

安装zend framework 2库

通过composer.phar 安装 ZF2 类库

cd /var/web/newzf2

1. 更新composer

php composer.phar self-update

2. 安装zf2类库

php composer.phar install

这个需要系统先安装git客户端。否则会提示,找不到git命令,安装完就可以了。

安装成功后,结果如下:

[/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

zendframework/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 zendframework/zendservice-recaptcha (ZendService\ReCaptcha for rendering ReCaptchas in Zend\Captcha and/or Zend\Form)
Writing lock file
Generating autoload files

安装的路径:/var/web/newzf2/vendor/zendframework/zendframework

用 composer.phar 安装 ZF2 类库比起 zftool.phar 有个好处就是解决依赖性的问题

项目框架生成完成

配置虚机到 /var/web/newzf2/public 就可以实现 访问。默认是访问Application模块

 

创建新模块 module

用 zftool.phar 可以创建我们所需要的模块(Module)

语法:

php zftool.phar create module <name> [<path>]
<name>              要创建的模块(Module)名称
<path>              ZF2应用程序根目录(可选)
结果如下:
[/var/web]# php zftool.phar create module user /var/web/newzf2/
The module User has been created in /var/web/newzf2

目前/var/web/newzf2/module/目录下有:

Application
User

查看/var/web/newzf2/config/application.config.php的内容:

<?php
/**
 * Configuration file generated by ZFTool
 * The previous configuration file is stored in application.config.old
 *
 * @see https://github.com/zendframework/ZFTool
 */
return array(
    'modules' => array(
        'Application',
        'User'
        ),
    'module_listener_options' => array(
        'module_paths' => array(
            './module',
            './vendor'
            ),
        'config_glob_paths' => array('config/autoload/{,*.}{global,local}.php')
        )
    );

添加了User module字段。

丰富module的内容,添加controller等

为了创建module内的内容,需要将zftool.phar文件copy或者mv到项目文件夹/var/web/newzf2,这是为了创建controller和config等方便。

创建控制器 controller

语法:

1 php zftool.phar create controller <name> <module>
2  
3 <name>              要创建的控制器(Controller)名称
4 <module>            控制器(Controller)所在的模块(Module)名称

在/var/web/newzf2下执行命令 :php zftool.phar create controller Index User

结果如下:

[/var/web/newzf2]# php zftool.phar create controller Index User 
PHP Fatal error:  Uncaught exception 'Zend\ModuleManager\Exception\RuntimeException' with message 'Module (ZFTool) could not be 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(90): 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

失败的原因在于没有找到和初始化ZFTool模块(Module)!

接下来就说说如何修复这个错误.

将ZFTool安装成模块(Module)

还记得一开始下载的ZFTOOL模块吗,现在要用到它了。复制整个 ZFTool 目录到项目所在目录中的 vendor 子目录下,复制完以后目录就够如下:

解压ZFTool到/var/web/newzf2/vendor目录下。

如下:

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

这样ZFTool已经安装成了模块了。

再次创建控制器成功

[/var/web/newzf2]# php zftool.phar create controller Index User
The controller Index has been created in module User.

需要说明,虽然controller已经生成了,但是目前还是不可用的,因为user/config/module.config.php这个自动生成的配置文件,内容为空数组,需要修改加入规则:

<?php
return array(
    'controllers' => array(
        'invokables' => array(
            'User\Controller\Index' => 'User\Controller\IndexController',
        ),
    ),
    'router' => array(
        'routes' => array(
            'user' => array(
                'type'    => 'Literal',
                'options' => array(
                    // Change this 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,
                    // you may want to remove it and replace it with more
                    // specific routes.
                    'default' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '/[:controller[/:action]]',
                            'constraints' => array(
                                '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',
        ),
    ),
);

ZFtool的其它命令

在介绍了 ZFTool 最主要的几个命令后,接下来说说其它的几个命令

 

1、显示调用的模块(Module)

语法:

1 php zftool.phar modules [list]           显示调用的模块(modules)

实例:

1 php zftool.phar modules

结果:

1 Modules installed:
2 Application
3 User

显示当前 ZF2 版本号

语法:

1 php zftool.phar version | --version      显示当前 Zend Framework 版本号

实例

1 php zftool.phar version

结果:

1 ZFTool - Zend Framework 2 command line Tool
2 The application in this folder is using Zend Framework 2.2.2

3、诊断一个模块(Module)

语法:

1 php zftool.phar diag [options] [module name]
2  
3 [module name]       (可选)要测试的模块名称
4  
5 [options]
6 -v --verbose        显示详细的信息
7 -b --break          在第一个失败的地方停止测试
8 -q --quiet          除非有错误,不然不显示任何内容
9 --debug             从测试中显示原始的调试(Debug)信息

实例:显示 Application 模块的详细信息

4、显示和设置应用程序配置信息

语法:

1 php zftool.phar config list                  展示所有的配置信息
2 php zftool.phar config get <name>            显示单个的配置信息,例如:"config get db.host"
3 php zftool.phar config set <name> <value>    设置一个配置值(只能修改一个数值)

5、Classmap 生成器

语法:

1 php zftool.phar classmap generate <directory> <classmap file> [--append|-a] [--overwrite|-w]
2  
3 <directory>         The directory to scan for PHP classes (use "." to use current directory)
4 <classmap file>     File name for generated class map file  or - for standard output. If not supplied, defaults to
5                     autoload_classmap.php inside <directory>.
6 --append | -a       Append to classmap file if it exists
7 --overwrite | -w    Whether or not to overwrite existing classmap file

结束。

下次介绍通过ZendSkeletonModule-master来快速生成自己的module。

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐