异常期间打印查看

发布于 2024-12-10 22:47:34 字数 585 浏览 0 评论 0原文

愚蠢的问题:在我的代码中,我收集了要在缓冲区中的视图中显示的所有消息。在下面的代码中,如果我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转身泪倾城 2024-12-17 22:47:34

我不喜欢这种方法!
在我的应用程序中,如果捕获了某些内容,则意味着该应用程序已损坏,然后记录错误并重定向到视图类型“哎呀......抱歉”!
当我打印到“消息”视图时,始终对应用程序流的固有逻辑很重要,让我决定消息的类型(成功或失败)而不是一个问题!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文