1

我阅读了几篇使用 ZF2 创建宁静 Web 服务的教程。我看到 ZF2 如何处理 RESTful Web 服务的最后一次更改发生在 2.0.4 版本中。让我开始的最有希望的文章是: http: //dustint.com/post/543/getting-started-with-abstractrestfulcontroller

无论如何,我无法完成它,在我看来,在 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\View\Exception\RuntimeException' 并带有消息 'Zend\View\Renderer\PhpRenderer::render: Unable to render template "application/rest/get-list"; 解析器无法解析到第 499 行 C:\xampp\htdocs\EhcServer\vendor\zendframework\zendframework\library\Zend\View\Renderer\PhpRenderer.php 中的文件

提前致谢!

4

2 回答 2

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',
    ),
),
于 2013-05-03T17:13:02.333 回答
0

If you are working on the Abstract RestfulConroller, simply

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

will make right, because the json itself enough to show in the rest methods,

$array = array();

return new JsonModel($array);

Thanks,

于 2015-03-20T07:15:23.813 回答