Zend_Controllerを使ったWebアプリケーションで、リクエストを処理し、クライアントにレスポンスを返す一連の流れの主な通過点を理解するため、ログを出力してみます。
Zend_Front_Controllerに追加できるプラグインという機能を利用します。
自前のプラグインの導入
Zend_Controller_Plugin_Abstractを継承して自前のプラグインクラスを作成します。
postDispatchメソッドで、リクエストオブジェクトのディスパッチフラグを強制的にリセット(setDispatched(false))し、別のアクション(testアクション)にディスパッチされるように設定しています。
BASEDIR/webapp02/MyAppPlugin.php
-
<?php
-
-
class MyAppPlugin extends Zend_Controller_Plugin_Abstract
-
{
-
private $_registry = null;
-
private $_logger = null;
-
private $_dispatchCount = 0;
-
-
function __construct()
-
{
-
$this->_registry = Zend_Registry::getInstance();
-
$this->_logger = $this->_registry['logger'];
-
}
-
-
public function routeStartup(Zend_Controller_Request_Abstract $request)
-
{
-
// 一回のリクエストで一回のみ
-
$this->_logger->log('MyAppPlugin::routeStartup()',Zend_Log::DEBUG);
-
}
-
-
public function routeShutdown(Zend_Controller_Request_Abstract $reques)
-
{
-
// 一回のリクエストで一回のみ
-
$this->_logger->log('MyAppPlugin::routeShutdown()',Zend_Log::DEBUG);
-
}
-
-
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
-
{
-
// ここで将来DB接続してみるつもり。
-
//$this->_registry['isDbError'] = true;
-
-
// 一回のリクエストで一回のみ
-
$this->_logger->log('MyAppPlugin::dispatchLoopStartup()',Zend_Log::DEBUG);
-
}
-
-
public function preDispatch(Zend_Controller_Request_Abstract $request)
-
{
-
if ($this->_dispatchCount> 10) {
-
// 現ソースでは、ここにくることはない(と思う)
-
$request->setDispatched(true);
-
return;
-
}
-
-
// 仮にDBエラーフラグがあった場合、ErrorコントローラーのdbErrorアクションを実行する
-
if ($request->getControllerName() != 'default' &&
-
$request->getControllerName() != 'error' &&
-
$request->getActionName() != 'dbError') {
-
$this->getResponse()->clearBody();// 仮に出力したものがあっても、削除
-
$request->setControllerName('error');
-
$request->setActionName('dbError');
-
}
-
}
-
-
$this->_logger->log('MyAppPlugin::preDispatch()',Zend_Log::DEBUG);
-
}
-
-
public function postDispatch(Zend_Controller_Request_Abstract $request)
-
{
-
$this->_dispatchCount++;
-
// ---- testアクションを処理するようにしてみるテスト
-
if ($i == 0) {
-
$request->setDispatched(false);// ディスパッチフラグをリセットしてみる
-
$request->setActionName('test');// testアクションを処理するように変更
-
$i++;
-
}
-
// -----
-
}
-
$this->_logger->log('MyAppPlugin::postDispatch(). count:' . $this->_dispatchCount,Zend_Log::DEBUG);
-
}
-
-
public function dispatchLoopShutdown()
-
{
-
// 一回のリクエストで一回のみ
-
$this->_logger->log('MyAppPlugin::dispatchLoopShutdown()',Zend_Log::DEBUG);
-
$this->_logger->log('*********************',Zend_Log::DEBUG);
-
}
-
}
このプラグインをフロントコントローラに登録します。
BASEDIR/webapp02/bootstrap.php
-
// 略
-
require_once $webapp_dir . '/MyAppPlugin.php';
-
$front->registerPlugin(new MyAppPlugin());
-
-
try {
-
$front->dispatch();
-
}
-
catch (Exception $exception) {
-
v($exception);
-
}
-
// 略
アクションコントローラのpreDispatch、postDispatchもオーバーライドして、ログ出力します。
webapp02/modules/default/controllers/IndexController.php
-
class IndexController extends Zend_Controller_Action
-
{
-
// 略
-
public function preDispatch()
-
{
-
parent::preDispatch();
-
-
$this->_logger->debug('IndexController::preDispatch(). action: ' . $this->getRequest()->getActionName());
-
}
-
-
public function postDispatch()
-
{
-
parent::postDispatch();
-
-
$this->_logger->debug('IndexController::postDispatch(). action: ' . $this->getRequest()->getActionName());
-
}
-
}
サンプルの実行
http;//サンプルインストールドメイン/にアクセスすると、defaultモジュール/indexコントローラ/indexアクションが処理された後、強制的にdefaultモジュール/indexコントローラ/testアクションが処理されることがわかります。
DEBUG (7): MyAppPlugin::routeStartup()
DEBUG (7): MyAppPlugin::routeShutdown()
DEBUG (7): MyAppPlugin::dispatchLoopStartup()
DEBUG (7): MyAppPlugin::preDispatch()
DEBUG (7): IndexController::init(). action: index
DEBUG (7): IndexController::preDispatch(). action: index
DEBUG (7): IndexController::indexAction()
DEBUG (7): IndexController::postDispatch(). action: index
DEBUG (7): MyAppPlugin::postDispatch(). count:1
DEBUG (7): MyAppPlugin::preDispatch()
DEBUG (7): IndexController::init(). action: test
DEBUG (7): IndexController::preDispatch(). action: test
DEBUG (7): IndexController::testAction()
DEBUG (7): IndexController::postDispatch(). action: test
DEBUG (7): MyAppPlugin::postDispatch(). count:2
DEBUG (7): MyAppPlugin::dispatchLoopShutdown()
僕は、アプリケーション全体で必要な共通の処理は、プラグインを使用するつもりです。
例えば、プラグインのdispatchLoopStartup()で、DBへの接続を行うことなどを考えています。
プラグインを2個登録してみる
複数のプラグインを登録した場合、デフォルトでは、プラグインの各メソッドは、フロントコントローラに登録(registerPlugin)した順番に起動されます。
(registerPluginの第2引数で順番を指定できます。)
例えば、次のようにプラグインを登録した場合、プラグインのpreDispatchメソッドは、MyAppPlugin::preDispatch()起動された後に、MySecondPlugin::preDispatch()メソッドが起動されます。
-
$front->registerPlugin(new MyAppPlugin());
-
$front->registerPlugin(new MySecondPlugin());
サンプルで、2個のプラグインを登録した場合のログ出力は、次のようになります。
DEBUG (7): MyAppPlugin::routeStartup()
DEBUG (7): MySecondPlugin::routeStartup()
DEBUG (7): MyAppPlugin::routeShutdown()
DEBUG (7): MySecondPlugin::routeShutdown()
DEBUG (7): MyAppPlugin::dispatchLoopStartup()
DEBUG (7): MySecondPlugin::dispatchLoopStartup()
DEBUG (7): MyAppPlugin::preDispatch()
DEBUG (7): MySecondPlugin::preDispatch()
DEBUG (7): IndexController::init(). action: index
DEBUG (7): IndexController::preDispatch(). action: index
DEBUG (7): IndexController::indexAction()
DEBUG (7): IndexController::postDispatch(). action: index
DEBUG (7): MyAppPlugin::postDispatch(). count:1
DEBUG (7): MySecondPlugin::postDispatch(). count:1
DEBUG (7): MyAppPlugin::preDispatch()
DEBUG (7): MySecondPlugin::preDispatch()
DEBUG (7): IndexController::init(). action: test
DEBUG (7): IndexController::preDispatch(). action: test
DEBUG (7): IndexController::testAction()
DEBUG (7): IndexController::postDispatch(). action: test
DEBUG (7): MyAppPlugin::postDispatch(). count:2
DEBUG (7): MySecondPlugin::postDispatch(). count:2
DEBUG (7): MyAppPlugin::dispatchLoopShutdown()
DEBUG (7): MySecondPlugin::dispatchLoopShutdown()
サンプルダウンロード
ダウンロードページにアーカイブしたファイルを置きました。
実際に動くものを作って、Zend Frameworkのマニュアル(7.2. Zend_Controller の基本)をみると、理解が深まると思います。