0
votes

I want to bundle the whole error handling in one module. It's working so far, but the error template is still the default [project root]/module/Application/view/error/index.phtml. I want to use it, but wrapped by my additional code. For this I need to add the default exception template as a partial. It's not possible (anymore) to pass the module name to the Partial view helper. So I tried this (suggested here):

[project root]/module/ErrorHandling/view/exception.phtml

echo $this->partial('Application/error/index');

But it didn't work:

Zend\View\Exception\RuntimeException: Zend\View\Renderer\PhpRenderer::render: Unable to render template "Application/error/index"; resolver could not resolve to a file in /var/www/.../my-project/vendor/zendframework/zend-view/src/Renderer/PhpRenderer.php:494

It works only for views from the /module/{AnotherModule}/view/{another-module} subfolders.

How to get a view from another module (but outside the /module/{AnotherModule}/view/{another-module} subfolder) rendered as a partial?

2

2 Answers

0
votes

A workaround is to add the desired view to the template map of the View Manager, e.g.:

config/autoload/global.php

return [
    'view_manager' => [
        'template_map' => [
            'default_zf_error_view' => __DIR__ . '/../../module/Application/view/error/index.phtml'
        ]
    ]
];

It works, but it still would be interesting to know, how to solve this issue a bit cleaner way.

0
votes

You don't have to add it in global.php

It will be enough to add it to the module.config.php of the module. Those views will be also accessible inside the other modules because ZF2 merges all config files of your different modules. Read more on this here in the documentation in the chapter Advanced Configuration Tricks.

So in module.config.php:

return [

    //...

    'view_manager' => [
        'template_map' => [
            'default_zf_error_view' => __DIR__ . '/../../module/Application/view/error/index.phtml'
        ]
    ]

    //...

];

In the controller of any other module you can now do:

$viewModel->setTemplate('default_zf_error_view');