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 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler