ZF2: Access FlashMessenger from view helper

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

ZF2: Access FlashMessenger from view helper

Ralf Eggert
Hi,

Is it possible to access the FlashMessenger from a view helper in ZF2? I
want to write a view helper that checks the FlashMessenger for any
messages and outputs them if any exist.

Regards,

Ralf

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: ZF2: Access FlashMessenger from view helper

Ralf Eggert
Hi again,

additional question. Is it possible to access a view helper from an
action controller?

Best regards,

Ralf

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

RE: ZF2: Access FlashMessenger from view helper

demiankatz
In reply to this post by Ralf Eggert
You can simply pass the whole FlashMessenger object to the ViewModel.

In my application, I do this by always constructing ViewModels through a createViewModel method in my base controller which sets up the FlashMessenger association:

http://vufind.git.sourceforge.net/git/gitweb.cgi?p=vufind/vufind;a=blob;f=module/VuFind/src/VuFind/Controller/AbstractBase.php;h=672473a3df9e9bd25844ada523c03730f38399c5;hb=HEAD

...and then I have a custom View Helper which pulls data out of the FlashMessenger, displays it appropriately and clears the messages:

http://vufind.git.sourceforge.net/git/gitweb.cgi?p=vufind/vufind;a=blob;f=module/VuFind/src/VuFind/Theme/Root/Helper/Flashmessages.php;h=7f62451dc735ddaaad1be1ffd9c501a2466d4910;hb=HEAD

This allows me to display messages consistently on all screens without having to think too hard about it.  I use namespaces that correspond with <div> classes so I can assign messages to different priorities -- hence all the looping in my helper.

It's entirely possible that this solution can be improved -- in particular, it would be nice to avoid the need for always calling the createViewModel method.  I'm open to suggestions...  but as it stands, it works well enough for my needs.

- Demian
________________________________________
From: Ralf Eggert [[hidden email]]
Sent: Friday, August 24, 2012 5:01 AM
To: Zend Framework General
Subject: [fw-general] ZF2: Access FlashMessenger from view helper

Hi,

Is it possible to access the FlashMessenger from a view helper in ZF2? I
want to write a view helper that checks the FlashMessenger for any
messages and outputs them if any exist.

Regards,

Ralf

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]



--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

RE: ZF2: Access FlashMessenger from view helper

demiankatz
In reply to this post by Ralf Eggert
As long as your controllers implement the ServiceLocatorAwareInterface (and they probably do already), you can do it like this:

$helper = $this->getServiceLocator()->get('viewmanager')->getRenderer()->plugin('helper-name');

Perhaps there is a more direct route, but this is how I've been doing it.

Also, it probably goes without saying, but I think using view helpers in a controller is not generally recommended.  However, I also understand that sometimes you can't easily avoid it (though it may be a sign that something ought to be refactored).

- Demian
________________________________________
From: Ralf Eggert [[hidden email]]
Sent: Friday, August 24, 2012 5:03 AM
To: [hidden email]
Subject: Re: [fw-general] ZF2: Access FlashMessenger from view helper

Hi again,

additional question. Is it possible to access a view helper from an
action controller?

Best regards,

Ralf

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]



--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: ZF2: Access FlashMessenger from view helper

Ralf Eggert
In reply to this post by demiankatz
Hi Demian,

thanks for your thoughts and the given example. I was able to reduce the
passing of the FlashMessenger to a oneliner:

$this->getEvent()->getViewModel()->setVariable(
    'flashMessenger', $this->flashMessenger()
);

Thanks,

Ralf

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: ZF2: Access FlashMessenger from view helper

Ralf Eggert
In reply to this post by demiankatz
Hi Demian,

thanks again and I understand your reservation. Actually I won't
probably need it.

I will pass the FlashMessenger to the View Model and pass other messages
as well. My view helper then takes all these messages and echoes them.
Like you I want them to be printed always on the same position of my layout.

Regards,

Ralf

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: ZF2: Access FlashMessenger from view helper

Jurian Sluiman
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: ZF2: Access FlashMessenger from view helper

Ralf Eggert
In reply to this post by Ralf Eggert
Hi again,

actually, I don't get it. My view helper ShowMessages() should be called
within my layout script. But I have big problems to pass any variable to
the layout script. I had a look at the Layout Controller Plugin, but
that did not get me any further. I looked through the reference guide
but did not find any answer to it.

This leads me to two questions:

How can I pass variables to be accessible within my layout script?

How can I access the variables from the layout script within a view
helper that is called in this layout script?

Thanks and best regards,

Ralf

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: ZF2: Access FlashMessenger from view helper

Ralf Eggert
In reply to this post by Jurian Sluiman
Hi Jurian,

I only read your mail after sending my last one, since it was trapped in
my spam folder.

I will give it a try now, although I am still interested in answers for
my questions from the other mail (14:29).

Regards,

Ralf

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

RE: ZF2: Access FlashMessenger from view helper

demiankatz
In reply to this post by Ralf Eggert
Views are rendered using a hierarchy of ViewModel objects -- the layout only has direct access to the top level, and the ViewModels created by your controller actions are attached as children of the layout...

If you need to set values directly in the top-level layout, you can use the layout() controller plug-in.

- Demian
________________________________________
From: Ralf Eggert [[hidden email]]
Sent: Friday, August 24, 2012 8:29 AM
To: [hidden email]
Subject: Re: [fw-general] ZF2: Access FlashMessenger from view helper

Hi again,

actually, I don't get it. My view helper ShowMessages() should be called
within my layout script. But I have big problems to pass any variable to
the layout script. I had a look at the Layout Controller Plugin, but
that did not get me any further. I looked through the reference guide
but did not find any answer to it.

This leads me to two questions:

How can I pass variables to be accessible within my layout script?

How can I access the variables from the layout script within a view
helper that is called in this layout script?

Thanks and best regards,

Ralf

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]



--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: ZF2: Access FlashMessenger from view helper

Ralf Eggert
Hi Demian,

> Views are rendered using a hierarchy of ViewModel objects -- the layout only has direct access to the top level, and the ViewModels created by your controller actions are attached as children of the layout...
>
> If you need to set values directly in the top-level layout, you can use the layout() controller plug-in.

I tried that before and it didn't work. But then I found my mistake. I
tried to access the layout plugin in the constructor of my action
controller. At that time the plugin it seems that the plugin is not
ready to use.

But now it works.

Regards,

Ralf

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: ZF2: Access FlashMessenger from view helper

Ralf Eggert
In reply to this post by Jurian Sluiman
Hi Jurian,

I got it to work. But factory config part took me a while to get it up
and running. It should look like this:

public function getViewHelperConfig()
{
    return array(
        'factories' => array(
            'ShowMessages' =>  function($sm) {
                $plugin = $sm->getServiceLocator()
                             ->get('ControllerPluginManager')
                             ->get('flashMessenger');
                $helper = new ShowMessages($plugin);
                return $helper;
            },
        ),
    );
}

Within the getViewHelperConfig method() the ControllerPluginManager
needs to be accessed via the service locator.

Regards,

Ralf

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: ZF2: Access FlashMessenger from view helper

weierophinney
Administrator
In reply to this post by Ralf Eggert
-- Ralf Eggert <[hidden email]> wrote
(on Friday, 24 August 2012, 02:29 PM +0200):

> Hi again,
>
> actually, I don't get it. My view helper ShowMessages() should be called
> within my layout script. But I have big problems to pass any variable to
> the layout script. I had a look at the Layout Controller Plugin, but
> that did not get me any further. I looked through the reference guide
> but did not find any answer to it.
>
> This leads me to two questions:
>
> How can I pass variables to be accessible within my layout script?

From your controller:

    $this->layout()->var = $value;

> How can I access the variables from the layout script within a view
> helper that is called in this layout script?

    $this->view->var


--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: ZF2: Access FlashMessenger from view helper

Ralf Eggert
Hi again,

>>From your controller:
>
>     $this->layout()->var = $value;
>
>> How can I access the variables from the layout script within a view
>> helper that is called in this layout script?
>
>     $this->view->var

Thanks again. The problem was due to the fact that I tried to pass
variables to the layout within the action controller constructor. That
does not work and that confused me.

Best regards,

Ralf

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]