我可以在 init() 方法中取消 Zend 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?
}
}
}
我当前的解决方法是创建一个空的 nullAction()
方法并调用 $this->_forward('null')
转发到它。
During Zend_Controller_Action::init()
, is there a way to cancel the action (so it won't be called)?
<?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?
}
}
}
My current workaround is to make an empty nullAction()
method and call $this->_forward('null')
to forward to it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 init() 中使用 $this->_forward() 没有什么问题(如果你想要转发的方法与 init() 在同一个控制器中),这只会改变请求对象的控制器/操作(覆盖通过路由器设置的内容)。
另一种方法是创建一个 Zend_Controller_Plugin,因为它看起来像是您正在处理意外错误。
查看 Zend_Controller_Plugin_ErrorHandler 实现。
因此,您不必转发到另一个操作,而是抛出异常,并让您的自定义插件在 postDispatch() 中检查响应是否包含任何期望,并且如果它确实简单地编辑当前的 Request 对象以显示您的“取消”操作。
最后一种方法可能是在 echo "{error: "invalid json"}" 之后抛出退出,这不是很好,但您将避免再次调度迭代的过载。
Theres nothing wrong in using $this->_forward() inside the init() (if the method you want to forward to is inside the same controller as the init()), this will simply change the request object's controller / action (overwrite what has been set via the router).
An alternative to this would be to create a Zend_Controller_Plugin as it looks like you are handling unexpected errors.
Have a look at the Zend_Controller_Plugin_ErrorHandler implementation.
So instead of forwarding to another action you would throw an exception, and have your custom plugin check in postDispatch() if the response contains eny expections, and if it does simply edit the current Request object to show your "cancel" action.
The last way could be to just throw a exit after the echo "{error: "invalid json"}" not very nice but you will avoid the overload of having another dispatch iteration.
我会抛出一个异常(然后在错误控制器中捕获它) - 这就是出现不可恢复错误时所做的事情。
I would throw an exception (and then catch it in error controller) - that's what you do when there is non-recoverable error.
也许试试这个:
Maybe try this:
对于仍在寻找解决方案的人,您可以尝试以下操作:
添加一个标志来说明是否存在错误并覆盖调度方法。如果设置了该标志,则不要调用
parent::dispatch
。这按照我想要的方式工作。我的原因是我正在制作一个抽象 API 控制器,默认情况下它会检查请求中的 API 密钥(如果不存在) - 控制器应该响应错误并成功完成请求。
例如,
对我来说,这似乎是一个干净的解决方案。
For someone that is still looking for solution you can try this:
Add a flag stating whether there is an error or not and override dispatch method. If the flag is set, do not invoke
parent::dispatch
.This works the way I want it to. My reason was that I am making an abstract API controller, which by default checks for an API key in request, if its not there - controller should respond with error and complete request successfully.
E.g.
For me this seems like a clean solution.
根据文档(在“调度前和调度后”下) Hooks"),在 preDispatch() 中调用 _forward() 将跳过该操作。
具体来说,preDispatch() 是这样的:“它的主要目的是决定是否应该分派所请求的操作。如果不分派,您应该 _forward() 到另一个操作,或者抛出异常。”
According to the documentation (under "Pre- and Post-Dispatch Hooks"), calling _forward() in preDispatch() will skip the action.
Specifically about preDispatch(): "its primary purpose is to make decisions about whether or not the requested action should be dispatched. If not, you should then _forward() to another action, or throw an exception."