使用flashMessenger的Zend Framework 2 redirect()不起作用

时间:2015-12-15 10:43:59

标签: php redirect zend-framework2 flash-message zend-controller-plugin

我有一些带有method_1()的控制器。在这个方法中,我调用method_2()。在method_2()中,我有(尝试... catch) - 使用已定义的flashMesseges和重定向进行阻止。

$this->flashMessenger()->addErrorMessage("There are errors.");
return $this->redirect()->toRoute('home');

但它不起作用。但如果我写作

$this->redirect()->toRoute('home');
$this->flashMessenger()->addErrorMessage("There are errors.");

一切都好。在method_1()代码中

$this->flashMessenger()->addErrorMessage("There are errors.");
return $this->redirect()->toRoute('home');
好的工作。我不明白。有人能帮助我吗?

A类 - 重定向不起作用。并且消息添加到会话。

class A {
  public function manageAction()
  {
      $view = new ViewModel();

      $form = $this->getForm();
      $form = $this->fillForm($form);

      $view->form = $form;
      return $view;
  }

  public function fillForm($form)
  {
      try {
          // ...
      } catch (\Exception $e) {
          $this->flashMessenger()->addErrorMessage("Error");
          return $this->redirect()->toRoute('home');
      }
      return $form;
  }
}

B类 - 重定向工作。并打印消息。

class B {
  public function manageAction()
  {
      $view = new ViewModel();

      $form = $this->getForm();
      $form = $this->fillForm($form);

      $view->form = $form;
      return $view;
  }

  public function fillForm($form)
  {
      try {
          // ...
      } catch (\Exception $e) {
          $this->redirect()->toRoute('home');
          $this->flashMessenger()->addErrorMessage("Error");
      }
      return $form;
  }
}

为什么以及如何运作?

2 个答案:

答案 0 :(得分:1)

redirect()插件返回Response个对象。你应该在Action中返回它。

  

更新:我建议将try/catch移至操作。

class A {
  public function manageAction()
  {
      $view = new ViewModel();

      $form = $this->getForm();

      try {
         $this->fillForm($form);
      } catch (\Exception $e) {
          $this->flashMessenger()->addErrorMessage("Error");
          return $this->redirect()->toRoute('home');
      }

      $view->form = $form;
      return $view;
  }

  public function fillForm($form)
  {
          // ...
  }
}

答案 1 :(得分:1)

插件FlashMessenger ,将您的消息发送到等待池(通过FlashMessenger Zend MVC 插件),该消息将显示在另一个页面请求中(通过 ViewHelper < / strong> FlashMessenger)。

有4种类型的消息可以与 Bootstrap 通知集成(错误,信息,默认,成功)。

现在让我们练习

控制器中的操作中,您必须输入消息和品牌:

use Zend\Mvc\Controller\Plugin\FlashMessenger;

public function registerAction(){
  if($formValid){
      $this->flashMessenger()->addSucessMessage('Saved!');
  } else{
      $this->flashMessenger()->addErrorMessage('Fail!');
  }

  //redirect to other route and show message
  return $this->redirect()->toRoute('app');
}

查看(.phtml)中,您只需使用:

#show messages of addErrorMessage();
echo $flash->render('error',   array('alert', 'alert-dismissible', 'alert-danger'));
#show messages of addInfoMessage();
echo $flash->render('info',    array('alert', 'alert-dismissible', 'alert-info'));
#show messages of addMessage();
echo $flash->render('default', array('alert', 'alert-dismissible', 'alert-warning'));
#show messages of addSucessMessage();
echo $flash->render('success', array('alert', 'alert-dismissible', 'alert-success'));

查看中,如果使用引导程序

 $flash = $this->flashMessenger();
 $flash->setMessageOpenFormat('<div>
     <button type="button" class="close" data-dismiss="alert" aria-hidden="true">
         &times;
     </button>
     <ul><li>')
     ->setMessageSeparatorString('</li><li>')
     ->setMessageCloseString('</li></ul></div>');


 echo $flash->render('error',   array('alert', 'alert-dismissible', 'alert-danger'));
 echo $flash->render('info',    array('alert', 'alert-dismissible', 'alert-info'));
 echo $flash->render('default', array('alert', 'alert-dismissible', 'alert-warning'));
 echo $flash->render('success', array('alert', 'alert-dismissible', 'alert-success'));

现在是一个黑客,如果你想在屏幕上查看 FlashMessages 而不需要重新定位页面(非常适合表单错误,你没有重定向或AJAX到另一个页面),请使用renderCurrent并且清楚。

echo $ flash-&gt; renderCurrent('error',array('alert','alert-dismissible','alert-danger'));

如果您想更深入地了解它,请按照官方Zend 2文档链接,提供一个可用的方法,将有很多帮助:

查看 - &gt; http://framework.zend.com/manual/current/en/modules/zend.view.helpers.flash-messenger.html

CONTROLLER - &gt; http://framework.zend.com/manual/current/en/modules/zend.mvc.plugins.html#zend-mvc-controller-plugins-flashmessenger