简体   繁体   中英

How to get this simple ZF2 view resolver to work

Here's my code:

$renderer = new \Zend\View\Renderer\PhpRenderer();
$resolver = new \Zend\View\Resolver\TemplatePathStack(array(
    'script_paths' => array(
        '/module/B2bCore/view/b2b-core/std-email')));
$renderer->setResolver($resolver);

$view = new \Zend\View\Model\ViewModel();
$view->setTemplate("{$this->template}.phtml")->setTerminal(true);
$content = $renderer->render($view);

This throws an error. For example, when the value of $this->template is org/invite.phtml I get the following:

Zend\View\Renderer\PhpRenderer::render: Unable to render template "org/invite.phtml"; resolver could not resolve to a file

My file path and name is as follows:

/module/B2bCore/view/b2b-core/std-email/org/invite.phtml

I see that the examples in the manual specify the script paths using the __DIR__ magic constant. I would prefer to stick away from this and reference everything from the application root (which is set per the sample module index.php file). I think that the config above is in line with this, but still it doesn't work.

You are not really supposed to write paths relative to your application in your ZF2 modules.

In your example, "/module..." is recognized as an absolute path (starts with a / ), and realpath() obviously reduces it to false , which is why you see an exception message telling you something about "org/invite.phtml" .

Since your package/module contains the views that are to be rendered, you should always use __DIR__ to reference them, as you don't know what the application's path is (from a "vendor" perspective).

Your view resolver should probably be instantiated with something like following (adjust the path):

$resolver = new \Zend\View\Resolver\TemplatePathStack([
    'script_paths' => [
        __DIR__ . '/../../view/b2b-core/std-email'
    ]
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM