[zf2 beta5] How to perform operations on generated HTML?

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

[zf2 beta5] How to perform operations on generated HTML?

Jonathan Maron
Hello all

I have a number of components, which perform operations on all of the
generated HTML.

For example, one component writes the "generated at" date as a comment
to the end of the HTML page. Another one rewrites all <img /> src
attributes to point to a CDN etc.

Previous to zf2 beta5, I achieved this as follows:

namespace Application

class Module [..]

public function init(ModuleManager $moduleManager)
{
    $sharedManager = $moduleManager->events()->getSharedManager();

    $sharedManager->attach('Zend\Mvc\Application', 'finish',
            array('My\X\GeneratedAt', 'execute'), 2);

    $sharedManager->attach('Zend\Mvc\Application', 'finish',
            array('My\X\Cdn',         'execute'), 3);
}

Where My\X\GeneratedAt and My\X\Cdn perform their operations on the HTML.

How can I achieve the same functionality in ZF2 beta5?

TIA

Jonathan
Reply | Threaded
Open this post in threaded view
|

Re: [zf2 beta5] How to perform operations on generated HTML?

HHGAG
Hi Jonathan,

that should quite work,  try it with the alias 'application'  instead of 'Zend\Mvc\Application'

Hasan H. Gürsoy (HHGAG)

Jonathan Maron <[hidden email]> schrieb:

>Hello all
>
>I have a number of components, which perform operations on all of the
>generated HTML.
>
>For example, one component writes the "generated at" date as a comment
>to the end of the HTML page. Another one rewrites all <img /> src
>attributes to point to a CDN etc.
>
>Previous to zf2 beta5, I achieved this as follows:
>
>namespace Application
>
>class Module [..]
>
>public function init(ModuleManager $moduleManager)
>{
>    $sharedManager = $moduleManager->events()->getSharedManager();
>
>    $sharedManager->attach('Zend\Mvc\Application', 'finish',
>            array('My\X\GeneratedAt', 'execute'), 2);
>
>    $sharedManager->attach('Zend\Mvc\Application', 'finish',
>            array('My\X\Cdn',         'execute'), 3);
>}
>
>Where My\X\GeneratedAt and My\X\Cdn perform their operations on the HTML.
>
>How can I achieve the same functionality in ZF2 beta5?
>
>TIA
>
>Jonathan
Hasan H. Gürsoy (HHGAG)
Reply | Threaded
Open this post in threaded view
|

Re: [zf2 beta5] How to perform operations on generated HTML?

Jonathan Maron
Hello Hasan

Thanks for your assistance. It turns out, the solution is as follows.
How this helps someone:

public function onBootstrap(\Zend\Mvc\MvcEvent $e)
{
    $sharedManager =
$e->getApplication()->getEventManager()->getSharedManager();

    $sharedManager->attach('Zend\Mvc\Application', 'finish',
            array('My\X\GeneratedAt', 'execute'), 2);

    $sharedManager->attach('Zend\Mvc\Application', 'finish',
            array('My\X\Cdn',         'execute'), 3);

}

Jonathan

On Tue, Jul 10, 2012 at 11:21 AM, Hasan H. Gürsoy
<[hidden email]> wrote:

> Hi Jonathan,
>
> that should quite work,  try it with the alias 'application'  instead of 'Zend\Mvc\Application'
>
> Hasan H. Gürsoy (HHGAG)
>
> Jonathan Maron <[hidden email]> schrieb:
>
>>Hello all
>>
>>I have a number of components, which perform operations on all of the
>>generated HTML.
>>
>>For example, one component writes the "generated at" date as a comment
>>to the end of the HTML page. Another one rewrites all <img /> src
>>attributes to point to a CDN etc.
>>
>>Previous to zf2 beta5, I achieved this as follows:
>>
>>namespace Application
>>
>>class Module [..]
>>
>>public function init(ModuleManager $moduleManager)
>>{
>>    $sharedManager = $moduleManager->events()->getSharedManager();
>>
>>    $sharedManager->attach('Zend\Mvc\Application', 'finish',
>>            array('My\X\GeneratedAt', 'execute'), 2);
>>
>>    $sharedManager->attach('Zend\Mvc\Application', 'finish',
>>            array('My\X\Cdn',         'execute'), 3);
>>}
>>
>>Where My\X\GeneratedAt and My\X\Cdn perform their operations on the HTML.
>>
>>How can I achieve the same functionality in ZF2 beta5?
>>
>>TIA
>>
>>Jonathan
Reply | Threaded
Open this post in threaded view
|

Re: [zf2 beta5] How to perform operations on generated HTML?

HHGAG
In reply to this post by Jonathan Maron
Hi Jonathan,

my fault,  your first one will also work with

getEventManager()  

instead of

events()

I've overseen that.  But onBootstrap is ok in this case

Hasan H. Gürsoy (HHGAG)

Jonathan Maron <[hidden email]> schrieb:

>Hello Hasan
>
>Thanks for your assistance. It turns out, the solution is as follows.
>How this helps someone:
>
>public function onBootstrap(\Zend\Mvc\MvcEvent $e)
>{
>    $sharedManager =
>$e->getApplication()->getEventManager()->getSharedManager();
>
>    $sharedManager->attach('Zend\Mvc\Application', 'finish',
>            array('My\X\GeneratedAt', 'execute'), 2);
>
>    $sharedManager->attach('Zend\Mvc\Application', 'finish',
>            array('My\X\Cdn',         'execute'), 3);
>
>}
>
>Jonathan
>
>On Tue, Jul 10, 2012 at 11:21 AM, Hasan H. Gürsoy
><[hidden email]> wrote:
>> Hi Jonathan,
>>
>> that should quite work,  try it with the alias 'application'  instead of 'Zend\Mvc\Application'
>>
>> Hasan H. Gürsoy (HHGAG)
>>
>> Jonathan Maron <[hidden email]> schrieb:
>>
>>>Hello all
>>>
>>>I have a number of components, which perform operations on all of the
>>>generated HTML.
>>>
>>>For example, one component writes the "generated at" date as a comment
>>>to the end of the HTML page. Another one rewrites all <img /> src
>>>attributes to point to a CDN etc.
>>>
>>>Previous to zf2 beta5, I achieved this as follows:
>>>
>>>namespace Application
>>>
>>>class Module [..]
>>>
>>>public function init(ModuleManager $moduleManager)
>>>{
>>>    $sharedManager = $moduleManager->events()->getSharedManager();
>>>
>>>    $sharedManager->attach('Zend\Mvc\Application', 'finish',
>>>            array('My\X\GeneratedAt', 'execute'), 2);
>>>
>>>    $sharedManager->attach('Zend\Mvc\Application', 'finish',
>>>            array('My\X\Cdn',         'execute'), 3);
>>>}
>>>
>>>Where My\X\GeneratedAt and My\X\Cdn perform their operations on the HTML.
>>>
>>>How can I achieve the same functionality in ZF2 beta5?
>>>
>>>TIA
>>>
>>>Jonathan
Hasan H. Gürsoy (HHGAG)
Reply | Threaded
Open this post in threaded view
|

Re: [zf2 beta5] How to perform operations on generated HTML?

weierophinney
Administrator
In reply to this post by Jonathan Maron
-- Jonathan Maron <[hidden email]> wrote
(on Tuesday, 10 July 2012, 12:40 PM +0200):

> Thanks for your assistance. It turns out, the solution is as follows.
> How this helps someone:
>
> public function onBootstrap(\Zend\Mvc\MvcEvent $e)
> {
>     $sharedManager =
> $e->getApplication()->getEventManager()->getSharedManager();
>
>     $sharedManager->attach('Zend\Mvc\Application', 'finish',
>             array('My\X\GeneratedAt', 'execute'), 2);
>
>     $sharedManager->attach('Zend\Mvc\Application', 'finish',
>             array('My\X\Cdn',         'execute'), 3);
>
> }

By the way, there's actually no reason to use the shared event manager
in this instance. Since you're grabbing the EM from the application
instance already, just register directly with it.

    public function onBootstrap(\Zend\Mvc\MvcEvent $e)
    {
        $events = $e->getApplication()->getEventManager();
        $events->attach('finish', array('My\X\GeneratedAt', 'execute'), 2);
        $events->attach('finish', array('My\X\Cdn',         'execute'), 3);
    }


> On Tue, Jul 10, 2012 at 11:21 AM, Hasan H. Gürsoy
> <[hidden email]> wrote:
> > Hi Jonathan,
> >
> > that should quite work,  try it with the alias 'application'  instead of 'Zend\Mvc\Application'
> >
> > Hasan H. Gürsoy (HHGAG)
> >
> > Jonathan Maron <[hidden email]> schrieb:
> >
> >>Hello all
> >>
> >>I have a number of components, which perform operations on all of the
> >>generated HTML.
> >>
> >>For example, one component writes the "generated at" date as a comment
> >>to the end of the HTML page. Another one rewrites all <img /> src
> >>attributes to point to a CDN etc.
> >>
> >>Previous to zf2 beta5, I achieved this as follows:
> >>
> >>namespace Application
> >>
> >>class Module [..]
> >>
> >>public function init(ModuleManager $moduleManager)
> >>{
> >>    $sharedManager = $moduleManager->events()->getSharedManager();
> >>
> >>    $sharedManager->attach('Zend\Mvc\Application', 'finish',
> >>            array('My\X\GeneratedAt', 'execute'), 2);
> >>
> >>    $sharedManager->attach('Zend\Mvc\Application', 'finish',
> >>            array('My\X\Cdn',         'execute'), 3);
> >>}
> >>
> >>Where My\X\GeneratedAt and My\X\Cdn perform their operations on the HTML.
> >>
> >>How can I achieve the same functionality in ZF2 beta5?
> >>
> >>TIA
> >>
> >>Jonathan
>

--
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
Reply | Threaded
Open this post in threaded view
|

Re: [zf2 beta5] How to perform operations on generated HTML?

Jonathan Maron
> By the way, there's actually no reason to use the shared event manager
> in this instance. Since you're grabbing the EM from the application
> instance already, just register directly with it.
>
>     public function onBootstrap(\Zend\Mvc\MvcEvent $e)
>     {
>         $events = $e->getApplication()->getEventManager();
>         $events->attach('finish', array('My\X\GeneratedAt', 'execute'), 2);
>         $events->attach('finish', array('My\X\Cdn',         'execute'), 3);
>     }

Thank you Matthew for the suggestion. This does, indeed, make more sense.

;-)

Jonathan