2013-05-03 44 views
1

我阅读了几个使用ZF2创建平稳web服务的教程。我看到ZF2在2.0.4版本中处理稳定Web服务的最新变化。最有前途的文章,让我开始是这样的: http://dustint.com/post/543/getting-started-with-abstractrestfulcontroller使用Zend Framework 2.1的REST风格的webservice * - 不显示JsonModel

无论如何,我不能把它做,在我看来,在RestController.getList()返回我是JsonModel不工作像预期。由于我的调试调用,我可以识别出我的RestController.getList()方法将被调用。所有相关的代码是在我的github存储库位置: https://github.com/Jochen1980/EhcServer/blob/master/module/Application/src/Application/Controller/RestController.php

class RestController extends AbstractRestfulController{ 
    public function indexAction(){ 
     Debug::dump("indexAction()"); 
     return new ViewModel(); 
    } 
    public function getList() { 
     Debug::dump("getList()"); 
     return new JsonModel(array(
      array('name' => 'test'), 
      array('name' => 'second') 
     )); 
    } 
    ... 

目前我得到这个错误信息: 致命错误:未捕获的异常“的Zend \查看\异常\ RuntimeException的”有消息“的Zend \查看\渲染\ PhpRenderer :: render:无法呈现模板“application/rest/get-list”;解决程序无法解析到文件'在C:\ xampp \ htdocs \ EhcServer \ vendor \ zendframework \ zendframework \ library \ Zend \ View \ Renderer \ PhpRenderer.php在线499

在此先感谢!

回答

5

strategies需要在里面view_managermodule.config.php

即,视图管理器部分应该是这样的

'view_manager' => array(
    'display_not_found_reason' => true, 
    'display_exceptions' => true, 
    'doctype' => 'HTML5', 
    'not_found_template' => 'error/404', 
    'exception_template' => 'error/index', 
    'template_map' => array(
     'layout/layout' => __DIR__ . '/../view/layout/layout.phtml', 
     'application/index/index' => __DIR__ . '/../view/application/index/index.phtml', 
     'error/404' => __DIR__ . '/../view/error/404.phtml', 
     'error/index' => __DIR__ . '/../view/error/index.phtml', 
    ), 
    'template_path_stack' => array(
     __DIR__ . '/../view', 
    ), 
    // let the view manager know which strategies to use 
    'strategies' => array(
     'ViewJsonStrategy', 
    ), 
), 
+0

Zend \ View \ Renderer \ PhpRenderer :: render:无法呈现模板? – Crusader 2015-03-13 15:16:54

0

如果你正在使用的抽象RestfulConroller,只需

'view_manager' => array(
    // let the view manager know which strategies to use 
    'strategies' => array(
     'ViewJsonStrategy', 
    ), 
), 

会做出正确的,因为json本身就足以显示其余的方法,

$ array = array();

return new JsonModel($ array);

谢谢,