异常期间打印查看
愚蠢的问题:在我的代码中,我收集了要在缓冲区中的视图中显示的所有消息。在下面的代码中,如果我的 Try{ } 失败并直接转到 Catch{ },错误将被缓冲,但控制器将没有机会分配给视图,对吗?遇到这种情况大家会怎么解决呢?
MyController
{
//...
try {
// ... do something
$this->_messages[] = array('success', 'Thank You.');
}
catch (Exception $e) {
$this->_messages[] = array('error', 'Oops! There was an error.');
}
/**
* flush all buffered messages to the view
*/
$flashmsg = $this->_flashMessenger->getMessages();
if (!empty($flashmsg)) $this->_messages[] = $flashmsg[0];
$this->view->messages = $this->_messages;
}
Silly question: in my code I collect all the messages to be displayed in the view in a buffer. On the code bellow if my Try{ } fails and go straight to Catch{ } the error will be buffered but the controller wont have the chance to assign to the view, right? How would you guys solve this kind of situation?
MyController
{
//...
try {
// ... do something
$this->_messages[] = array('success', 'Thank You.');
}
catch (Exception $e) {
$this->_messages[] = array('error', 'Oops! There was an error.');
}
/**
* flush all buffered messages to the view
*/
$flashmsg = $this->_flashMessenger->getMessages();
if (!empty($flashmsg)) $this->_messages[] = $flashmsg[0];
$this->view->messages = $this->_messages;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不喜欢这种方法!
在我的应用程序中,如果捕获了某些内容,则意味着该应用程序已损坏,然后记录错误并重定向到视图类型“哎呀......抱歉”!
当我打印到“消息”视图时,始终对应用程序流的固有逻辑很重要,让我决定消息的类型(成功或失败)而不是一个问题!
I do not like this approach!
In my application if something goes into the catch means that the application is broken, then log the error and redirect to view type "Oops ... Sorry"!
When I print to the Messages view, are always significant to the inherent logic of the application flow, for me to decide the type of message (success or bad) and not a catch!