Domanda

I have a some view helpers throwing exceptions on error. That's ok for development, but for production I would like to configure PhpRenderer to catch and log the exception without braking the hole view file to render - simply return nothing.

The PhpRenderer has the method:

public function __call($method, $argv)
{
    if (!isset($this->__pluginCache[$method])) {
        $this->__pluginCache[$method] = $this->plugin($method);
    }
    if (is_callable($this->__pluginCache[$method])) {
        return call_user_func_array($this->__pluginCache[$method], $argv);
    }
    return $this->__pluginCache[$method];
}

Is it enough to overwrite this method?

How can I replace the PhpRenderer with my own?

È stato utile?

Soluzione

You can do it by writing your own view strategy.

First, register your new strategy in the configuration.

return array(
        'factories' => array(
            'MyStrategy' => 'Application\ViewRenderer\StrategyFactory',
        )
        'view_manager' => array(
               'strategies' => array(
                   'MyStrategy'
                ), 
         ),
);  

Then, create your own strategy

namespace Application\ViewRenderer;

use Zend\View\Strategy\PhpRendererStrategy;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\FactoryInterface;

class StrategyFactory implements FactoryInterface
{


    public function createService (ServiceLocatorInterface $serviceLocator)
    {
        $renderer = new Renderer ();

        return new Strategy ($renderer);
    }
}

and renderer.

namespace Application\ViewRenderer;
use Zend\View\Renderer\PhpRenderer;

class Renderer extends PhpRenderer
{
    public function render($nameOrModel, $values = null) {
            // this is just an example
            // the actual implementation will be very much like in PhpRenderer
        return 'x';
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top