I tried to render template file in Zend Framework 2.5 AbstractRestfulController but something wrong or missing in code. What can I do ?
Controller code
use 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;
}
}
Module folders
- Api
-- config
-- src
--- Api
---- Controller
----- TrialController.php
-- view
--- api
--- trial
---- specialty_talktrack.phtml
Error
"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"
template_map
Your template files should be defined in your view_manager
config inside a key template_map
. You can read more on this in the documentation for Zend\View
.
//...
'view_manager' => array(
'template_map' => array(
'api/trial/specialty_talktrack' => ...path to your file...
)
),
//...
From the Zend\View
docs:
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',
),
template_path_stack
You can also check the example from the ZF2 album application tutorial where they set a template_path_stack
which is like a default folder to use for searching for template files. If you want to search by default for views in your view folder you could add this path to your template_path_stack
as follows:
'view_manager' => array(
'template_path_stack' => array(
'Api' => __DIR__ . '/../view',
),
),
From the Zend\View
docs:
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',
),