Search code examples
phpzend-frameworkzend-framework2zf3

Zend Framework, PhpRenderer unable to render template adds controllers into the template name


I migrated from ZF2 to ZF3. Now I have a problem. My view script is in the right place, the configuration seems ok, but I get the following error:

Zend\View\Renderer\PhpRenderer::render: Unable to render template "parties/controllers/write-party/add"; resolver could not resolve to a file

which is a fairly common error to solve, but the problem is that for some reason, I get controllers folder in the template path. The template path should be parties/write-party/add.

module.config.php

return [
    'controllers' => [
        'factories' => [
            WritePartyController::class => WritePartyControllerFactory::class,
        ],
    ],
    'router' => [
        'routes' => [
            'parties' => [
                'type'    => Literal::class,
                'options' => [
                    'route'    => '/parties',
                    'defaults' => [
                        '__NAMESPACE__' => 'Parties\Controllers',
                        'controller'    => 'Index',
                        'action'        => 'index',
                    ],
                ],
                'may_terminate' => true,
                'child_routes' => [
                    'add' => [
                        'type' => Segment::class,
                        'options' => [
                            'route' => '/add',
                            'defaults' => [
                                'controller' => WritePartyController::class,
                                'action' => 'add',
                            ],
                        ],
                    ],
    //...
    'view_manager' => [
        'template_path_stack' => [
            'parties' => __DIR__ . '/../view',
        ],
        'strategies' => [
            'ViewJsonStrategy',
        ],
    ],
],

How to get the correct path to my view script which is parties/write-party/add?


Solution

  • The issue turned out to be more complex than I thought. Internally ZF3 assumes that the folder with the module's controllers is named Controller (singular). If that's not the case, it lets the namespace YourModule/Controllers/ControllersName/ to be transferred to the path your-module/controllers/controllers-name/ and that's what I saw. All this happens in the InjectTemplateListener's mapController method. So I guess it's a bug because things worked for ZF2 without a problem.