ゼロからzend framework 2プロジェクトフレームワークを作成する

12124 ワード

システム要件:php 5.3.3以上のバージョンのプロジェクトディレクトリ:/var/web/newzf 2
準備作業:zftoolをダウンロードします.pharおよびZFTOOLモジュール.へ/var/web zftool.pharアドレス:https://packages.zendframework.com/zftool.pharZFTOOLモジュールのアドレス:https://github.com/zendframework/ZFTool/zipball/master
参照http://framework.zend.com/manual/2.2/en/modules/zendtool.introduction.html.
zftool.pharは独立したファイルで、php 5を再利用することができます.3.3環境下でコマンドライン実行、zf 2のフレームワークの構築.まず、正常に動作するかどうかを見て、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

zf 2プロジェクトフレームワークの作成
構文:php zftool.phar create project ./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/newzf 2に入って生成されたディレクトリ構造を見てみましょう.
-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インストールZF 2クラスライブラリ
cd /var/web/newzf2

1.composerの更新
php composer.phar self-update

2.zf 2クラスライブラリのインストール
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/newzf 2/vendor/zendframework/zendframework
composerでpharはZF 2クラスライブラリをzftoolよりインストール.pharの利点は依存性の問題を解決することです
プロジェクトフレームワークの生成完了
ダミーを/var/web/newzf 2/publicに構成するとアクセスできます.アプリケーションモジュールへのアクセスはデフォルトです
 
新しいモジュールmoduleの作成
zftoolでpharは必要なモジュール(Module)を作成できます
構文:
php zftool.phar create module  []
                    (Module)  
              ZF2       (  )
[/var/web]# php zftool.phar create module user /var/web/newzf2/
The module User has been created in /var/web/newzf2

現在/var/web/newzf 2/module/ディレクトリには次のものがあります.
Application
User

/var/web/newzf 2/config/applicationを表示します.config.phpの内容:
 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は、controllerやconfigなどを作成するのに便利なプロジェクトフォルダ/var/web/newzf 2にあります.
コントローラコントローラの作成
構文:1 php zftool.phar create controller 2
  3               (Controller) 4             (Controller) (Module)
/var/web/newzf 2でコマンド: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をモジュールにインストールする
最初にダウンロードしたZFTOOLモジュールを覚えていますか.今使います.ZFToolディレクトリ全体をプロジェクトのあるディレクトリのvendorサブディレクトリにコピーします.コピーが完了すると、ディレクトリは次のようになります.
ZFToolを/var/web/newzf 2/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という自動生成されたプロファイルは、内容が空の配列であり、追加ルールを変更する必要があります.
 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
現在のZF 2バージョン番号を表示
構文:1 php zftool.phar version | --version      Zend Framework
≪インスタンス|Instance|emdw≫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             , : "config get db.host" 3 php zftool.phar config  set       ( )
5、Classmapジェネレータ
構文:1 php zftool.phar classmap generate file > [--append|-a] [--overwrite|-w] 2
  3          The directory to scan  for   PHP classes (use  "."   to use current directory) 4 file >     File name  for   generated class map  file    or -  for   standard output. If not supplied, defaults to 5                      autoload_classmap.php inside . 6 --append | -a       Append to classmap  file   if   it exists 7 --overwrite | -w    Whether or not to overwrite existing classmap  file
わります.
はZendSkeletonModule-masterで のModuleを く する をご します.