douxu2467
2016-11-14 11:13
浏览 499
已采纳

如何在zf2 AbstractRestfulController中调用模板文件

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"

图片转代码服务由CSDN问答提供 功能建议

我试图在Zend Framework 2.5 AbstractRestfulController中渲染模板文件,但代码中出现了错误或缺失。 我该怎么办?

控制器代码

使用Zend \ View \ Model \ ViewModel; \ n

 类试用扩展AbstractRestfulController {
 
公共函数DetailTalktrackAction(){
 
 $ view = new ViewModel(); 
 $ view-> setTemplate('api / trial /  special_talktrack'); 
 $ view-> setTerminal(true); 
 $ html = $ this-> getServiceLocator() - > get('viewrenderer') - > render($ view); 
 echo  $ html; 
退出; 
 
} 
 
} 
   
 
 

模块文件夹 \ n

   -  Api 
-- config 
-- src 
 --- Api 
 ---- Controller 
 ----- TrialController.php 
-- view 
-  -  api 
 --- trial 
 ---- specialty_talktrack.phtml 
   
 
 

错误 \ n

 “class”:“Zend \\ View \\ Exception \\ RuntimeException”,
“file”:“/ opt / lampp / htdocs / crush / phase2 / vendor / zendframework / zend-view /  src / Renderer / PhpRenderer.php“,
”line“:494,
”message“:”Zend \\ View \\ Render  er \\ PhpRenderer :: render:无法渲染模板\“api / trial / specialty_talktrack \”; 解析器无法解析为文件“
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doucheng1063 2016-11-14 11:34
    已采纳

    1) 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',
        ),
    

    2) 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',
        ),
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题