gpt4 book ai didi

php - 处理 Zend Framework 的 Controller 插件中抛出的异常

转载 作者:可可西里 更新时间:2023-11-01 13:48:37 24 4
gpt4 key购买 nike

我有一个扩展 Zend_Controller_Plugin_Abstract 的 Acl 插件,这个插件处理我所有的 Acl 代码。

我想在这个插件中抛出一个异常,例如Exception_Unauthorised 然后在我的 ErrorController 中处理它,这样我就可以为不同的应用程序使用相同的 Acl 插件并使用 ErrorController 来以不同方式处理每个应用程序中的每种情况 - 如果需要的话。

问题是在插件中抛出一个Exception并不会阻止原来的Action的执行。所以我最终得到了原始 Action 的输出和 ErrorController 输出。

如何在插件中抛出异常以阻止原始操作发生?

案例一

// 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");
}

案例二

// 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)
{

}
}

案例三

我认为这就是问题所在,通过更改 Controller 。

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 的 Controller 插件中抛出的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3548282/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com