如何在zf2 AbstractRestfulController中调用模板文件
How to call template file in zf2 AbstractRestfulController
我尝试在 Zend Framework 2.5 AbstractRestfulController 中呈现模板文件,但代码中出现错误或缺失。我能做什么?
控制器代码
使用Zend\View\Model\ViewModel;
class trial extends AbstractRestfulController{
public function DetailTalktrackAction(){
$view = new ViewModel();
$view->setTemplate('api/trial/specialty_talktrack');
$view->setTerminal(true);
$html = $this->getServiceLocator()->get('viewrenderer')->render($view);
echo $html;
exit;
}
}
模块文件夹
- Api
-- config
-- src
--- Api
---- Controller
----- TrialController.php
-- view
--- api
--- trial
---- specialty_talktrack.phtml
错误
"class": "Zend\View\Exception\RuntimeException",
"file": "/opt/lampp/htdocs/crush/phase2/vendor/zendframework/zend-view/src/Renderer/PhpRenderer.php",
"line": 494,
"message": "Zend\View\Renderer\PhpRenderer::render: Unable to render template \"api/trial/specialty_talktrack\"; resolver could not resolve to a file"
1) template_map
您的模板文件应该在您的 view_manager
配置中定义在一个键 template_map
内。你可以阅读更多关于这个 in the documentation for Zend\View
.
//...
'view_manager' => array(
'template_map' => array(
'api/trial/specialty_talktrack' => ...path to your file...
)
),
//...
来自 Zend\View
文档:
The TemplateMapResolver allows you to directly map template names
to specific templates. The following map would provide locations
for a home page template ("application/index/index"), as well as for
the layout ("layout/layout"), error pages ("error/index"), and
404 page ("error/404"), resolving them to view scripts.
'template_map' => array(
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'site/layout' => __DIR__ . '/../view/layout/layout.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
),
2) template_path_stack
您还可以检查 the example from the ZF2 album application tutorial 他们设置 template_path_stack
的地方,它类似于用于搜索模板文件的默认文件夹。如果您想在视图文件夹中默认搜索视图,您可以将此路径添加到您的 template_path_stack
,如下所示:
'view_manager' => array(
'template_path_stack' => array(
'Api' => __DIR__ . '/../view',
),
),
来自 Zend\View
文档:
The TemplatePathStack takes an array of directories. Directories
are then searched in LIFO order (it's a stack) for the requested
view script. This is a nice solution for rapid application
development, but potentially introduces performance expense in
production due to the number of static calls necessary.
The following adds an entry pointing to the view directory
of the current module. Make sure your keys differ between modules
to ensure that they are not overwritten -- or simply omit the key!
'template_path_stack' => array(
'application' => __DIR__ . '/../view',
),
我尝试在 Zend Framework 2.5 AbstractRestfulController 中呈现模板文件,但代码中出现错误或缺失。我能做什么?
控制器代码
使用Zend\View\Model\ViewModel;
class trial extends AbstractRestfulController{
public function DetailTalktrackAction(){
$view = new ViewModel();
$view->setTemplate('api/trial/specialty_talktrack');
$view->setTerminal(true);
$html = $this->getServiceLocator()->get('viewrenderer')->render($view);
echo $html;
exit;
}
}
模块文件夹
- Api
-- config
-- src
--- Api
---- Controller
----- TrialController.php
-- view
--- api
--- trial
---- specialty_talktrack.phtml
错误
"class": "Zend\View\Exception\RuntimeException",
"file": "/opt/lampp/htdocs/crush/phase2/vendor/zendframework/zend-view/src/Renderer/PhpRenderer.php",
"line": 494,
"message": "Zend\View\Renderer\PhpRenderer::render: Unable to render template \"api/trial/specialty_talktrack\"; resolver could not resolve to a file"
1) template_map
您的模板文件应该在您的 view_manager
配置中定义在一个键 template_map
内。你可以阅读更多关于这个 in the documentation for Zend\View
.
//...
'view_manager' => array(
'template_map' => array(
'api/trial/specialty_talktrack' => ...path to your file...
)
),
//...
来自 Zend\View
文档:
The TemplateMapResolver allows you to directly map template names to specific templates. The following map would provide locations for a home page template ("application/index/index"), as well as for the layout ("layout/layout"), error pages ("error/index"), and 404 page ("error/404"), resolving them to view scripts.
'template_map' => array(
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'site/layout' => __DIR__ . '/../view/layout/layout.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
),
2) template_path_stack
您还可以检查 the example from the ZF2 album application tutorial 他们设置 template_path_stack
的地方,它类似于用于搜索模板文件的默认文件夹。如果您想在视图文件夹中默认搜索视图,您可以将此路径添加到您的 template_path_stack
,如下所示:
'view_manager' => array(
'template_path_stack' => array(
'Api' => __DIR__ . '/../view',
),
),
来自 Zend\View
文档:
The TemplatePathStack takes an array of directories. Directories are then searched in LIFO order (it's a stack) for the requested view script. This is a nice solution for rapid application development, but potentially introduces performance expense in production due to the number of static calls necessary.
The following adds an entry pointing to the view directory of the current module. Make sure your keys differ between modules to ensure that they are not overwritten -- or simply omit the key!
'template_path_stack' => array(
'application' => __DIR__ . '/../view',
),