Type of Flash Messenger in Zend

后端 未结 4 542
遥遥无期
遥遥无期 2021-02-02 15:25

Is it possible or How can i give a type to a FlashMessage in Zend?

For example

/* This is a \"Success\" message */
$this -> _helper -> FlashMesseng         


        
相关标签:
4条回答
  • 2021-02-02 15:47

    Method signatures in Zend Framework 1.12.x for FlashMessenger:

    public function addMessage($message, $namespace = null)
    public function getMessages($namespace = null)
    public function hasMessages($namespace = null)
    public function clearMessages($namespace = null)
    

    So to set messages the following will work:

    /* success message */
    $this->_helper->flashMessenger()->addMessage('Post created!', 'success');
    
    /* error message */
    $this->_helper->flashMessenger()->addMessage('You have no permissions', 'error');
    

    And for the view, the following should work:

    <?php $flashMessenger = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
    
    <?php if ($flashMessenger->hasMessages('success')): ?>
        <div class="message success">
        <?php foreach ($flashMessenger->getMessages('success') as $msg): ?>
            <?php echo $msg ?>
        <?php endforeach; ?>
        </div>
    <?php endif; ?>
    
    <?php if ($flashMessenger->hasMessages('error')): ?>
        <div class="message error">
        <?php foreach ($flashMessenger->getMessages('error') as $msg): ?>
            <?php echo $msg ?>
        <?php endforeach; ?>
        </div>
    <?php endif; ?>
    
    0 讨论(0)
  • 2021-02-02 15:48

    It is possible. Sample implementation in described in this blog post:

    • Zend Framework: View Helper Priority Messenger | emanaton

    Excerpt:

    class AuthController extends Zend_Controller_Action {
      function loginAction() {
        . . .
        if ($this->_request->isPost()) {
          $formData = $this->_request->getPost();
          if ($this->view->form->isValid($formData)) {
            . . .
          } else {
            $this->view->priorityMessenger('Login failed.', 'error');
          }
        . . .
      }
    }
    
    0 讨论(0)
  • 2021-02-02 15:50

    I think the best way to do this is by using the flashmessenger namespaces:

    /* success message */
    $this->_helper->FlashMessenger()->setNamespace('success')->addMessage('Post created!');
    
    /* error message */
    $this->_helper->FlashMessenger()->setNamespace('error')->addMessage('You have no permissions');
    

    And then in your layout you can get the messages added to each namespace:

    <?php $flashMessenger = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
    
    <?php if ($flashMessenger->setNamespace('success')->hasMessages()): ?>
        <div class="message success">
        <?php foreach ($flashMessenger->getMessages() as $msg): ?>
            <?php echo $msg ?>
        <?php endforeach; ?>
        </div>
    <?php endif; ?>
    
    <?php if ($flashMessenger->setNamespace('error')->hasMessages()): ?>
        <div class="message error">
        <?php foreach ($flashMessenger->getMessages() as $msg): ?>
            <?php echo $msg ?>
        <?php endforeach; ?>
        </div>
    <?php endif; ?>
    
    0 讨论(0)
  • 2021-02-02 15:51

    At one time you used assoc arrays to do this... Im not ure if this is still current or not...

    /* This is a "Success" message */
    $this -> _helper -> FlashMessenger(array('success' => 'You are successfully created a post.')); 
    
    /* This is an "Error" message  */
    $this -> _helper -> FlashMessenger(array('error' => 'There is an error while creating post.'));
    
    /* This is just a "Notification" message */
    $this -> _helper -> FlashMessenger(array('notice' => 'Now you can see your Post'));
    
    0 讨论(0)
提交回复
热议问题