【发布时间】:2011-04-02 16:10:46
【问题描述】:
我有一个扩展 Zend_Controller_Plugin_Abstract 的 Acl 插件,这个插件处理我所有的 Acl 代码。
我想在这个插件中抛出一个异常,例如一个Exception_Unauthorised,然后在我的ErrorController 中处理这个问题,这样我可以为不同的应用程序使用相同的Acl 插件,并使用ErrorController 以不同的方式处理每个应用程序中的每种情况——如果需要的话。
问题是在插件中抛出异常并不会阻止原始Action执行。所以我最终得到了原始 Action 的输出和ErrorController 输出。
如何在插件中抛出异常以阻止原始操作发生?
案例 1
// This throws the `Exception_NoPermissions`, but it does not get caught by
// `ErrorController`
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
parent::preDispatch($request);
throw new Exception_NoPermissions("incorrect permissions");
}
案例 2
// This behaves as expected and allows me to catch my Exception
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
parent::preDispatch($request);
try
{
throw new Exception_NoPermissions("incorrect permissions");
}
catch(Exception_NoPermissions $e)
{
}
}
案例 3
我认为这就是问题所在,通过更改控制器。
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
parent::preDispatch($request);
// Attempt to log in the user
// Check user against ACL
if(!$loggedIn || !$access)
{
// set controller to login, doing this displays the ErrorController output and
// the login controller
$request->getControllerName("login");
}
}
【问题讨论】:
标签: php zend-framework plugins exception-handling