Ste*_*lay 6 php model-view-controller zend-framework controller
在期间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?
}
}
}
Run Code Online (Sandbox Code Playgroud)
我目前的解决方法是创建一个空nullAction()方法并调用$this->_forward('null')转发它.
在 init() 中使用 $this->_forward() 没有任何问题(如果您想要转发到的方法与 init() 位于同一控制器内),这只会更改请求对象的控制器/操作(覆盖通过路由器设置的内容)。
另一种方法是创建一个 Zend_Controller_Plugin,因为它看起来像是您正在处理意外错误。查看 Zend_Controller_Plugin_ErrorHandler 实现。因此,您不必转发到另一个操作,而是抛出异常,并让您的自定义插件在 postDispatch() 中检查响应是否包含任何期望,并且如果它确实简单地编辑当前的 Request 对象以显示您的“取消”操作。
$request->setDispatched(false)
->setControllerName("error")
->setActionName("jsonDecoding");
Run Code Online (Sandbox Code Playgroud)
最后一种方法可能是在 echo "{error: "invalid json"}" 之后抛出退出,这不是很好,但您将避免再次调度迭代的过载。
| 归档时间: |
|
| 查看次数: |
4689 次 |
| 最近记录: |