Getting Viewer from Zend Controller Plugin

In my controller, I have postDispatch for consolidating FlashMessenger messages:

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

I want to do this in a controller plugin.

UPDATE: I understand why I need this - I want to send my flash messages to JSON when I call the JSON context. If messages are not added to the View object, I do not receive messages.

I managed to get the messages in an array, but I don’t know how to pass them to the 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;
    }
}

Is a bonus question the right way to achieve this? Thank!

+3
source share
3 answers

I found your post looking the same. Based on this stream , there are two simple ways to achieve it.

: (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;
+12

, . FlashMessenger ; .

, flashmessenger ( ), ?:)

, . . noumenal. .

+1

If you just want to get this functionality in all your controllers, you can simply extend Zend_Controller_Action and create a new class containing your mail sending code.

0
source

Source: https://habr.com/ru/post/1790190/


All Articles