How to override default PhpRenderer in ZF3 MVC


plase how i can override PhpRenderer in ZF3 ? I need it to add some special dynamic logic …

What i did currently, but it doesn’t work at all (looks Zend\Mvc ignores this setting)

1/ i added to config/application.config.php

    'service_manager' => [
        'factories' => [
            \Application\View\Renderer\PhpRenderer::class => \Application\View\Renderer\Factory\PhpRendererFactory::class,
        'aliases' => [
            'ViewPhpRenderer'                            => \Application\View\Renderer\PhpRenderer::class,
            'ViewRenderer'                               => \Application\View\Renderer\PhpRenderer::class,
            'Zend\View\Renderer\RendererInterface'       => \Application\View\Renderer\PhpRenderer::class,

2/ added modules/Application/src/View/Renderer/PhpRenderer.php


namespace Application\View\Renderer;

class PhpRenderer extends \Zend\View\Renderer\PhpRenderer
	// .... later some stuff will be here

3/ added modules/Application/src/View/Renderer/Factory/PhpRendererFactory.php


namespace Application\View\Renderer\Factory;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use Application\View\Renderer\PhpRenderer;

class PhpRendererFactory implements FactoryInterface
	 * @param  ContainerInterface $container
	 * @param  string $name
	 * @param  null|array $options
	 * @return PhpRenderer
	public function __invoke(ContainerInterface $container, $name, array $options = null)
		$renderer = new PhpRenderer();

		return $renderer;

But it odesn’t work … Mvc still uses Zend\View\Renderer\PhpRenderer instead of my Application\View\Renderer\PhpRenderer

Anybody can help me ? Thanks for any advice …

Internally, Zend\Mvc\Service\ViewPhpRendererStrategyFactory pulls the renderer using the service name Zend\View\Renderer\PhpRenderer; if you assign your custom factory to that particular service name, you should be set.

Thanks for tip but this doesn’t work :frowning:

Tried this in application.config.php (and also in module.config.php), without success, MVC is still using it’s factory, not mine…

    'service_manager' => [
        'factories' => [
            'Zend\View\Renderer\PhpRenderer' => \Application\View\Renderer\Factory\PhpRendererFactory::class,

Where are you registering your factory? This may be an issue of order of precedence…

tried put it to config/application.config.php (i need override renderer in all application modules so i guess this is good place)

I was working with skeleton and did a quick test. Default renderer was successfully overridden by custom one using Zend\View\Renderer\PhpRenderer key in module config.

that’s strange… can you please provide exactly which code you placed where ? i will try do exactly same…

All I did was adding:

'service_manager' => [
	'factories' => [
		'Zend\View\Renderer\PhpRenderer' => Application\Factory\PhpRendererFactory::class


AAh finally i found what was problem… this is really strange. . it was working from very beginning … reason why i thought it is not working is, that when there is exception because of missing template file, it throws this:


Zend\View\Renderer\PhpRenderer::render: Unable to render template “back-office/custom/index/index”; resolver could not resolve to a file

So it looks like template is rendererd by default zend rendere - but it is NOT ! - when i put die() into my overrided renderer redner() method, i realized that it IS actually called :slight_smile:

Strange… anyway, thanks for help !!