ZF2: Implementing custom renderer strategy

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

ZF2: Implementing custom renderer strategy

henit
Hi.

I am trying to implement a custom renderer strategy (and later a custom viewmodel) for my zf2 application.

I follow the manual (http://packages.zendframework.com/docs/latest/manual/en/zend.view.html#zend.view.quick-start.usage.strategies) but I get an error message.

public function onBootstrap($e) {
    // Register a "render" event, at high priority (so it executes prior
    // to the view attempting to render)
    $app = $e->getParam('application');
    $app->getEventManager()->attach('render', array($this, 'registerStrategy'), 100);
}

public function registerStrategy($e) {
    $app      = $e->getTarget();
    $locator  = $app->getServiceManager();
    $view     = $locator->get('Zend\View\View');
    $strategy = $locator->get('MyOwn\View\Strategy\MyOwnStrategy');
   
    // Attach strategy, which is a listener aggregate, at high priority
    $view->getEventManager()->attach($strategy, 100);
}

Including or autoloading MyOwn\View\Strategy\MyOwnStrategy does not make a difference, because in either case i get:

Fatal error: Uncaught exception 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message 'Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for MyOwn\View\Strategy\MyOwnStrategy' in (...)/library/Zend/ServiceManager/ServiceManager.php on line 400

Anyone have an idea what I am doing wrong?
Reply | Threaded
Open this post in threaded view
|

Re: ZF2: Implementing custom renderer strategy

Marco Pivetta
Did you define how 'MyOwn\View\Strategy\MyOwnStrategy' should be
instantiated by your service manager?
This should happen either in an factory or in an invokable (or in an
abstract factory).

To try it out real quick, just define a service_manager.invokables key with
your strategy:

array(
    'service_manager' => array(
        'invokables' => array(
            'MyOwn\View\Strategy\MyOwnStrategy' =>
'MyOwn\View\Strategy\MyOwnStrategy',
        ),
    ),
),

This should do when working with the assumption that your `MyOwnStrategy`
has a simple constructor.

You can read more at
http://blog.evan.pro/introduction-to-the-zend-framework-2-servicemanager

Marco Pivetta

http://twitter.com/Ocramius

http://marco-pivetta.com



On 24 July 2012 07:59, henit <[hidden email]> wrote:

> Hi.
>
> I am trying to implement a custom renderer strategy (and later a custom
> viewmodel) for my zf2 application.
>
> I follow the manual
> (
> http://packages.zendframework.com/docs/latest/manual/en/zend.view.html#zend.view.quick-start.usage.strategies
> )
> but I get an error message.
>
> public function onBootstrap($e) {
>     // Register a "render" event, at high priority (so it executes prior
>     // to the view attempting to render)
>     $app = $e->getParam('application');
>     $app->getEventManager()->attach('render', array($this,
> 'registerStrategy'), 100);
> }
>
> public function registerStrategy($e) {
>     $app      = $e->getTarget();
>     $locator  = $app->getServiceManager();
>     $view     = $locator->get('Zend\View\View');
>     $strategy = $locator->get('MyOwn\View\Strategy\MyOwnStrategy');
>
>     // Attach strategy, which is a listener aggregate, at high priority
>     $view->getEventManager()->attach($strategy, 100);
> }
>
> Including or autoloading MyOwn\View\Strategy\MyOwnStrategy does not make a
> difference, because in either case i get:
>
> Fatal error: Uncaught exception
> 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message
> 'Zend\ServiceManager\ServiceManager::get was unable to fetch or create an
> instance for MyOwn\View\Strategy\MyOwnStrategy' in
> (...)/library/Zend/ServiceManager/ServiceManager.php on line 400
>
> Anyone have an idea what I am doing wrong?
>
>
>
> --
> View this message in context:
> http://zend-framework-community.634137.n4.nabble.com/ZF2-Implementing-custom-renderer-strategy-tp4655896.html
> Sent from the Zend MVC mailing list archive at Nabble.com.
>
> --
> List: [hidden email]
> Info: http://framework.zend.com/archives
> Unsubscribe: [hidden email]
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: ZF2: Implementing custom renderer strategy

henit
I tried that now, and it worked, however it seems like ServiceManager treats the new strategy class as a factory object. I get an "invalid factory was registered" error (ServiceManager.php line 767).

My new Strategy class is more or less a copy of AccessStrategy in the example in the doc (http://packages.zendframework.com/docs/latest/manual/en/zend.view.html#zend.view.quick-start.usage.strategies). I see that PhpRenderStrategy in ZF2 is not an instanceof FactoryInterface.. Is this not the right way to implement custom renderer strategy objects?
Reply | Threaded
Open this post in threaded view
|

Re: ZF2: Implementing custom renderer strategy

henit
This post has NOT been accepted by the mailing list yet.
Ops, I see I put it in 'factories' instead of 'invokables'. Seems to work now. Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: ZF2: Implementing custom renderer strategy

B. Kamer
In reply to this post by henit
Hello,

On 24 jul. 2012, at 07:59, henit wrote:

> Hi.
>
> I am trying to implement a custom renderer strategy (and later a custom
> viewmodel) for my zf2 application.
>
> I follow the manual
> (http://packages.zendframework.com/docs/latest/manual/en/zend.view.html#zend.view.quick-start.usage.strategies)
> but I get an error message.
>
> public function onBootstrap($e) {
>    // Register a "render" event, at high priority (so it executes prior
>    // to the view attempting to render)
>    $app = $e->getParam('application');
>    $app->getEventManager()->attach('render', array($this,
> 'registerStrategy'), 100);
> }
>
> public function registerStrategy($e) {
>    $app      = $e->getTarget();
>    $locator  = $app->getServiceManager();
>    $view     = $locator->get('Zend\View\View');
>    $strategy = $locator->get('MyOwn\View\Strategy\MyOwnStrategy');
>
>    // Attach strategy, which is a listener aggregate, at high priority
>    $view->getEventManager()->attach($strategy, 100);
> }
>
> Including or autoloading MyOwn\View\Strategy\MyOwnStrategy does not make a
> difference, because in either case i get:
>
> Fatal error: Uncaught exception
> 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message
> 'Zend\ServiceManager\ServiceManager::get was unable to fetch or create an
> instance for MyOwn\View\Strategy\MyOwnStrategy' in
> (...)/library/Zend/ServiceManager/ServiceManager.php on line 400
>
> Anyone have an idea what I am doing wrong?

I think the appropriate way is to define you strategy in standard configuration...

<?php
return array(
    'view_manager' => array(
        // Additional strategies to attach
        // These should be class names or service names of View strategy classes
        // that act as ListenerAggregates. They will be attached at priority 100,
        // in the order registered.
        'strategies' => array(
            'ViewJsonStrategy', // register JSON renderer strategy
            'OrYourOwn',
        ),
    ),
);

Keep in mind that there are some outstanding issues with rendering in the current master that might interfere here - not sure

>
>
>
> --
> View this message in context: http://zend-framework-community.634137.n4.nabble.com/ZF2-Implementing-custom-renderer-strategy-tp4655896.html
> Sent from the Zend MVC mailing list archive at Nabble.com.
>
> --
> 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: Implementing custom renderer strategy

henit
In reply to this post by Marco Pivetta
One more question. When setting a new strategy like the example above, how do the system cover arguments to the strategy class, like if I have a renderer as argument for the strategy class? I get an error message for missing argument when using this:

$strategy = $locator->get('MyOwn\View\Strategy\MyOwnStrategy');

The same happen if I try the built-in Zend\View\Strategy\JsonStrategy
Reply | Threaded
Open this post in threaded view
|

Re: ZF2: Implementing custom renderer strategy

insan3
This post has NOT been accepted by the mailing list yet.
Hey, I´ve started to implement a custom strategy just like you @henit and I have exactly the same question, now that the new strategy is responding to the Model how zf2 knows how to pass the right renderer? reflection? I think I`m missing something.

I got the following error:

Catchable fatal error: Argument 1 passed to Nmsp\View\Strategy\ViewPlainStrategy::__construct() must be an instance of Nmsp\View\Renderer\PlainRenderer, none given.