Getting View Object from within a Zend Controller plugin

眉间皱痕 提交于 2019-12-01 07:07:46

问题


In my controller, I have a postDispatch to consolidate my 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 make this into a Controller plugin.

UPDATE: I realized why I need this - I want to pass my flash messages in JSON when called by the JSON context. Unless the messages are added to the View object, I don't receive the messages.

I was able to get the messages into 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;
    }
}

Bonus question - is this the right way to accomplish this? Thanks!


回答1:


I found your post while searching for the same thing. Based on this thread, there are two simple ways to accomplish it.

One: If your view is initialized during bootstrap (resources.view[] = is in your application.ini), you can simply call this:

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

Two: If your view is not initialized during bootstrap:

$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
if (null === $viewRenderer->view) {
    $viewRenderer->initView();
}
$view = $viewRenderer->view;



回答2:


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.




回答3:


If you are just looking to get this functionality in all of your controllers, you can just extend the Zend_Controller_Action and make a new class containing your post dispatch code.



来源:https://stackoverflow.com/questions/4907490/getting-view-object-from-within-a-zend-controller-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!