从Zend Controller插件中获取视图对象 [英] Getting View Object from within a Zend Controller plugin

查看:81
本文介绍了从Zend Controller插件中获取视图对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我想将其放入Controller插件.

I want to make this into a Controller plugin.

更新:我意识到了我为什么需要这个-当我想通过JSON上下文调用Flash消息时,我希望以JSON形式传递我的Flash消息.除非将消息添加到View对象,否则我不会收到消息.

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!

推荐答案

我在搜索同一内容时发现了您的帖子.基于此线程,有两种简单的方法可以实现它.

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

一个::如果您在引导过程中初始化了视图(resources.view[] =在您的application.ini中),则可以简单地称呼它:

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;

这篇关于从Zend Controller插件中获取视图对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆