[Zend Framework] Zend_Tool: zf を利用してプロジェクトを作成する

推奨されるプロジェクト・ディレクトリ構造に沿った各ファイル類を生成してくれるコマンドラインツール。

[markdown]
Zend Server 利用時は、以下にコマンドがあります。

`/usr/local/zend/share/ZendFramework/bin/zf.sh`

ZendFramework を単体で利用した場合は、/bin 以下にあります。

`/ZendFramework/bin/zf.sh`

参考サイト

> * Zend_Tool – Zend Framework Reference – Zend Framework
> * 推奨されるプロジェクト・ディレクトリ構造 – Zend Framework MVC アプリケーションのために推奨されるプロジェクト構造 – Zend Framework
> * コマンドラインからのPHPアプリケーション管理 - Zend_Tool – (1/4):CodeZine
> * なんとなくメモ: Zend Framework1.10.0のZend_Toolを試してみた

## zf create project

例えば home ディレクトリの projects 以下に、vhsp というプロジェクトを作成したい場合。

“`
% zf create project ~/projects/vhsp
Creating project at /Users/あなたの名前/projects/vhsp
Note: This command created a web project, for more information setting up your VHOST, please see docs/README
Testing Note: PHPUnit was not found in your include_path, therefore no testing actions will be created.
“`

Zend Server –
アプリケーションの動作確認をする(3) | deadwood

“`
.
├── .zfproject.xml
├── application
│   ├── Bootstrap.php
│   ├── configs
│   │   └── application.ini
│   ├── controllers
│   │   ├── ErrorController.php
│   │   └── IndexController.php
│   ├── models
│   └── views
│   ├── helpers
│   └── scripts
│   ├── error
│   │   └── error.phtml
│   └── index
│   └── index.phtml
├── docs
│   └── README.txt
├── library
├── public
│   ├── .htaccess
│   └── index.php
└── tests
├── application
│   └── controllers
│   └── IndexControllerTest.php
├── bootstrap.php
├── library
└── phpunit.xml
16 directories, 13 files
“`

## 生成されたファイル(一部)

重要そうなファイルの内容を見て勉強してみる。
分からない点もまだまだ多いです。

### public/.htaccess

公開ディレクトリに配置されるファイル 1/2

“`:public/.htaccess
RewriteEngine On
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ – [NC,L]
# The following rewrites all other queries to index.php. The
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::$
RewriteRule ^(.*)$ – [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
“`

いつのまにか少し変わってました。

> リライトエンジンを起動
> ファイルが存在し、有限なサイズを持っているか
> パス名として存在し、かつシンボリックリンクであるか
> 存在し、かつディレクトリである場合
> 上記の条件に一つでも当てはまった場合は何もしないで終了
> それ以外の場合はindex.phpへ
>
> 流留~流れゆく記憶のログ ZendFrameworkの.htaccess

SetEnv APPLICATION_ENV developmentphp_value include_path ".:/ZendFramework/library" のような記述も、設計によってはすることもありそう。

Zend_Application – エラーの確認方法 | deadwood

Zend Server – アプリケーションの動作確認をする(3) | deadwood
waiting.

### public/index.php

公開ディレクトリに配置されるファイル 2/2

“`:public/index.php
bootstrap()
->run();
“`

公開ディレクトリの位置を変更した場合、APPLICATION_PATH を書き換えることになる。
waiting.

### application/configs/application.ini

重要なファイル 1/2
リソースプラグインを利用して、共通の設定情報をまとめることができます。
APPLICATION_ENV の設定に応じて、production, staging, testing, development に書かれた設定を読み替えるようです。

利用できるリソースプラグイン – Zend_Application – Zend Framework

“`:application/configs/application.ini
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH “/../library”
bootstrap.path = APPLICATION_PATH “/Bootstrap.php”
bootstrap.class = “Bootstrap”
appnamespace = “Application”
resources.frontController.controllerDirectory = APPLICATION_PATH “/controllers”
resources.frontController.params.displayExceptions = 0
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
“`

production や development の設定が効いて、各環境でうまく動くのか確かめないといけない。

Zend_Mail – 設定を application.ini にまとめる | deadwood

### application/Bootstrap.php

重要なファイル 2/2

“`php:application/Bootstrap.php
_getParam(‘error_handler’);
if (!$errors || !$errors instanceof ArrayObject) {
$this->view->message = ‘You have reached the error page’;
return;
}
switch ($errors->type) {
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
// 404 error — controller or action not found
$this->getResponse()->setHttpResponseCode(404);
$priority = Zend_Log::NOTICE;
$this->view->message = ‘Page not found’;
break;
default:
// application error
$this->getResponse()->setHttpResponseCode(500);
$priority = Zend_Log::CRIT;
$this->view->message = ‘Application error’;
break;
}
// Log exception, if logger available
if ($log = $this->getLog()) {
$log->log($this->view->message, $priority, $errors->exception);
$log->log(‘Request Parameters’, $priority, $errors->request->getParams());
}
// conditionally display exceptions
if ($this->getInvokeArg(‘displayExceptions’) == true) {
$this->view->exception = $errors->exception;
}
$this->view->request = $errors->request;
}
public function getLog()
{
$bootstrap = $this->getInvokeArg(‘bootstrap’);
if (!$bootstrap->hasResource(‘Log’)) {
return false;
}
$log = $bootstrap->getResource(‘Log’);
return $log;
}
}
“`

Zend_Exception や Zend_Log あたりも含めて、ErrorController がどのように動いているのか。

### docs/README.txt

バーチャルホストの設定テンプレートが生成されていました。

Zend Server – アプリケーションの動作確認をする(3) | deadwood

“`:/doc

DocumentRoot “/Users/あなたの名前/projects/vhsp/public”
ServerName vhsp.dev
# This should be omitted in the production environment
SetEnv APPLICATION_ENV development

Options Indexes MultiViews FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all


“`

[/markdown]