我目前正在Zend 2的一个库工作,其中包括渲染视图脚本。渲染工作正常,我也能够在视图脚本中使用任何其他帮助程序,但是 basePath() 帮助程序抛出异常并显示消息:
No base path provided
我已经尝试在配置中设置 basePath,但它仅在控制器呈现的视图脚本中发生变化。如果相关,下面是呈现视图的代码:
// file: /vendor/mate/library/mate/Group/Functions/GetHtml.php
// $templateDir is either directing to /module/Application/view/group/<name>.phtml
// or to /vendor/mate/library/mate/view/group/<name>.phtml
$map = new Resolver'TemplateMapResolver(array(
'group/'.$type => $templateDir,
));
$stack = new Resolver'TemplatePathStack(array(
'script_paths' => array(
$viewDir,
)
));
$resolver->attach($map) // this will be consulted first
->attach($stack);
$groupModel = new ViewModel(array(
'elements' => $this->getGroup()->toArray(),
'groupElement' => $groupElement,
));
$groupModel->setTemplate('group/'.$type);
$groupHtml = $renderer->render($groupModel);
有没有人知道如何让 basePath 在我的视图脚本中工作?
必须显式设置帮助程序的基本路径。从渲染器中获取插件,并在渲染之前先设置基本路径:
// create your view model
$renderer->plugin('basePath')->setBasePath('/foo');
// render view model now
问题只是因为你正在创建一个新的、独立的 PhpRenderer 实例,而不是使用已经存在的实例。
如果您通过服务管理器获取渲染器而不是创建一个新的渲染器,则不需要任何此配置。