我阅读了几个教程,用ZF2创建一个安静的Web服务。我看到最后一次改变了ZF2如何处理版本2.0.4中发生的restful webservices。让我入门的最有希望的文章是这样的: http://dustint.com/post/543/getting-started-with-abstractrestfulcontroller
无论如何,我无法完成它,在我看来,在RestController.getList()中,我返回的JsonModel不能像预期的那样工作。由于我的调试调用,我可以识别我的RestController.getList() - 方法将被调用。所有相关代码都在我的github-repository中: 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 \ View \ Exception \ RuntimeException',消息'Zend \ View \ Renderer \ PhpRenderer :: render:无法呈现模板“application / rest / get-list”;解析器无法解析为第499行的C:\ xampp \ htdocs \ EhcServer \ vendor \ zendframework \ zendframework \ library \ Zend \ View \ Renderer \ PhpRenderer.php中的文件
提前致谢!
答案 0 :(得分:5)
您的strategies
需要位于view_manager
module.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',
),
),
答案 1 :(得分:0)
如果您正在使用Abstract RestfulConroller,只需
'view_manager' => array(
// let the view manager know which strategies to use
'strategies' => array(
'ViewJsonStrategy',
),
),
会做出正确的,因为json本身足以在其余方法中显示,
$ array = array();
返回新的JsonModel($ array);
谢谢,