gpt4 book ai didi

php - 我可以在 init() 方法中取消 Zend Controller 操作吗?

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

Zend_Controller_Action::init() 期间,有没有办法取消操作(这样它就不会被调用)?

<?php
class JsonApiController extends Zend_Controller_Action {
function init()
{
// try JSON decoding the raw request body
if ($jsonDecodingFailed) {
echo '{"error":"invalid JSON"}';
$this->_cancelAction(); // something like this exist?
}
}
}

我目前的解决方法是制作一个空的 nullAction() 方法并调用 $this->_forward('null') 转发给它。

最佳答案

在 init() 中使用 $this->_forward() 没有错(如果你想转发到的方法与 init() 在同一个 Controller 中),这只会改变请求对象的 Controller /操作(覆盖通过路由器设置的内容)。

另一种方法是创建一个 Zend_Controller_Plugin,因为它看起来像是在处理意外错误。查看 Zend_Controller_Plugin_ErrorHandler 实现。因此,您不会转发到另一个操作,而是抛出一个异常,并让您的自定义插件检查 postDispatch() 是否响应包含任何期望,如果它只是编辑当前 Request 对象以显示您的“取消”操作。

$request->setDispatched(false)
->setControllerName("error")
->setActionName("jsonDecoding");

最后一种方法可能是在 echo "{error: "invalid json"}"之后抛出一个 exit 不是很好但是你会避免有另一个调度迭代的过载。

关于php - 我可以在 init() 方法中取消 Zend Controller 操作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3971043/

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