2009年4月23日星期四

Zend Framework Plugin: 不同模块(Module)使用不同的错误控制器(ErrorController)

Zend Framework 的错误控制器提供了统一的错误处理方式, 参阅 http://framework.zend.com/manual/en/zend.controller.html#zend.controller.quickstart.go.errorhandler
  当我们工作于模块方式时,常常希望不同模块使用不同的错误处理,比如 default 模块使用默认ErrorController, 后台管理模块 admin 使用 Admin_ErrorController 提供不同的布局和更详细的错误显示。

Plugin 方式为我们提供了方便的设置途径,以下 plugin 设置每个 module 使用 模块里的 ErrorController, 如果模块不提供 ErrorController 则使用默认的 ErrorController:
require_once ('Zend/Controller/Plugin/Abstract.php');
class Mezi_Controller_Plugin_ErrorControllerSelector extends Zend_Controller_Plugin_Abstract
{
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        $front = Zend_Controller_Front::getInstance();

        //If the ErrorHandler plugin is not registered, bail out
        if( !($front->getPlugin('Zend_Controller_Plugin_ErrorHandler') instanceof Zend_Controller_Plugin_ErrorHandler) )
            return;

        $error = $front->getPlugin('Zend_Controller_Plugin_ErrorHandler');

        //Generate a test request to use to determine if the error controller in our module exists
        $testRequest = new Zend_Controller_Request_HTTP();
        $testRequest->setModuleName($request->getModuleName())
                    ->setControllerName($error->getErrorHandlerController())
                    ->setActionName($error->getErrorHandlerAction());

        //Does the controller even exist?
        if ($front->getDispatcher()->isDispatchable($testRequest)) {
            $error->setErrorHandlerModule($request->getModuleName());
        }
    }
}


启动时安装 plugin:
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new Mezi_Controller_Plugin_ErrorControllerSelector());




Zend Framework Plugin:自适应 magic_quotes_gpc 环境

推荐关闭 php.ini 的 magic_quotes_gpc 选项,一来可以提升性能,二来可以保证得到的原始数据,Zend_Db 会自动转义数据入库。并且新版本的 php 推荐配置都是默认关闭 magic_quotes_gpc 选项。
当然某些系统打开了 magic_quotes_gpc 选项,并且你也没有足够权限去修改这个配置,这些反斜杠可能给你造成困扰,我们可以用代码抵消修改。
Zend Framework 的 plugin 提供了很好的方式来做这些事情,而不需要改动我们去参数的每段代码。

/**
* A Zend Controller Plugin dedicated to undoing the damage of magic_quotes_gpc
* in systems where it is on.
*
* @author Ken
* @version $Id:$
*/
require_once ('Zend/Controller/Plugin/Abstract.php');
class Mezi_Controller_Plugin_StripMagicQuotes extends Zend_Controller_Plugin_Abstract
{
/**
* strip all slashes off $request parameters
*
* @param Zend_Controller_Request_Abstract $request
*/
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
$params = $request->getParams();
array_walk_recursive($params, array($this , '_stripSlashes'));
$request->setParams($params);

if ($request instanceof Zend_Controller_Request_Http) {
$this->_stripSlashesGPC();
}
}

/**
* strip all slashes from GPC
*/
protected function _stripSlashesGPC()
{
array_walk_recursive($_GET, array($this , '_stripSlashes'));
array_walk_recursive($_POST, array($this , '_stripSlashes'));
array_walk_recursive($_COOKIE, array($this , '_stripSlashes'));
}

/**
* callback strip the slashes off an item in the Params array
*
* @param string $value
* @param string $key
*/
protected function _stripSlashes (&$value, $key)
{
$value = stripslashes($value);
}
}


然后启动时安装 plugin, 仅在 magic_quotes_gpc 打开时需要处理:

$front = Zend_Controller_Front::getInstance();

if (get_magic_quotes_gpc()) {
require_once 'Mezi/Controller/Plugin/StripMagicQuotes.php';
$this->_front->registerPlugin(new Mezi_Controller_Plugin_StripMagicQuotes());
}


Plugin 非常漂亮的实现了我们的需求,以前写好的代码都无须改动。