Zend 中的 Flash Messenger 类型

发布于 2024-09-28 06:26:38 字数 432 浏览 4 评论 0原文

是否可能或我如何为 Zend 中的 FlashMessage 指定类型?

例如

/* This is a "Success" message */
$this -> _helper -> FlashMessenger('You are successfully created a post.'); 

/* This is an "Error" message  */
$this -> _helper -> FlashMessenger('There is an error while creating post.');

/* This is just a "Notification" message */
$this -> _helper -> FlashMessenger('Now you can see your Post');

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

For example

/* This is a "Success" message */
$this -> _helper -> FlashMessenger('You are successfully created a post.'); 

/* This is an "Error" message  */
$this -> _helper -> FlashMessenger('There is an error while creating post.');

/* This is just a "Notification" message */
$this -> _helper -> FlashMessenger('Now you can see your Post');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

黯然#的苍凉 2024-10-05 06:26:38

我认为最好的方法是使用 flashmessenger 命名空间:

/* success message */
$this->_helper->FlashMessenger()->setNamespace('success')->addMessage('Post created!');

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

然后在布局中,您可以将消息添加到每个命名空间:

<?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; ?>

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; ?>
酸甜透明夹心 2024-10-05 06:26:38

有一次你使用关联数组来做到这一点...我不确定这是否仍然是当前的...

/* 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'));

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'));
任性一次 2024-10-05 06:26:38

这是可能的。此博客文章中描述的示例实现:

摘录:

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');
      }
    . . .
  }
}

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

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');
      }
    . . .
  }
}
两人的回忆 2024-10-05 06:26:38

Zend Framework 1.12.x 中 FlashMessenger 的方法签名:

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

因此,要设置消息,以下内容将起作用:

/* success message */
$this->_helper->flashMessenger()->addMessage('Post created!', 'success');

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

对于视图,以下内容应该起作用:

<?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; ?>

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