3

在我的控制器中,我有一个 postDispatch 来整合我的 FlashMessenger 消息:

public function postDispatch()
{       
    $messages = $this->_helper->getHelper ( 'FlashMessenger' )
        ->getMessages ();

    if ( $this->_helper->getHelper ( 'FlashMessenger' )
        ->hasCurrentMessages () )
    {
        $messages = array_merge ( $messages, $this->_helper->getHelper ( 'FlashMessenger' )
            ->getCurrentMessages () );
        $this->_helper->getHelper ( 'FlashMessenger' )
            ->clearCurrentMessages ();
    }

    $this->view->alert = $messages;
}

我想把它做成一个控制器插件。

更新:我意识到我为什么需要这个 - 我想在 JSON 上下文调用时以 JSON 格式传递我的闪存消息。除非将消息添加到 View 对象,否则我不会收到消息。

我能够将消息放入数组中,但我不知道如何将它们传递给视图:

class Plugin_FlashMessenger extends Zend_Controller_Plugin_Abstract
{
    public function postDispatch($request)
    {
        $flashmessenger = Zend_Controller_Action_HelperBroker::getStaticHelper ( 'FlashMessenger' );

        $messages = $flashmessenger->getMessages ();
        if ( $flashmessenger->hasCurrentMessages () )
        {
            $messages = array_merge ( $messages, $flashmessenger->getCurrentMessages () );
            $flashmessenger->clearCurrentMessages ();
        }

        // THIS LINE IS WRONG. HOW DO I SEND $messages TO THE VIEW?
        $this->view->alert = $messages;
    }
}

奖金问题 - 这是实现这一目标的正确方法吗?谢谢!

4

3 回答 3

12

我在搜索相同内容时发现了您的帖子。基于这个线程,有两种简单的方法来完成它。

一:如果您的视图在引导期间被初始化(resources.view[] =在您的application.ini),您可以简单地调用它:

$view = Zend_Controller_Front::getInstance()
        ->getParam('bootstrap')
        ->getResource('view');

二:如果您的视图在引导期间未初始化:

$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
if (null === $viewRenderer->view) {
    $viewRenderer->initView();
}
$view = $viewRenderer->view;
于 2011-05-25T16:11:05.157 回答
1

I believe it's the wrong way to do it. The FlashMessenger is to have little notifications from one request to the next; available in the view.

Thus, the flashmessenger is already a controller action helper (for the above purpose) why do you want to build another helper on top of that? :)

So, your problem is actually getting the messages in the view. For that, there is already a view helper. From noumenal. It's awesome.

于 2011-02-05T16:22:14.070 回答
0

如果你只是想在你的所有控制器中获得这个功能,你可以扩展 Zend_Controller_Action 并创建一个包含你的 post dispatch 代码的新类。

于 2011-02-07T02:11:02.897 回答