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

如何在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"
  • 写回答

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',
        ),
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么