1
votes

I read several Tutorials to create a restful webservice with ZF2. I saw that the last changes how ZF2 handles restful webservices happened in version 2.0.4. The most promising article to get me started was this: http://dustint.com/post/543/getting-started-with-abstractrestfulcontroller

Anyway, I can't get it done, it seems to me, that in RestController.getList() my returned JsonModel is not working like expected. Due to my Debug-call I can recognize that my RestController.getList()-method will be invoked. All related code is in my github-repository here: 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')
        ));
    } 
    ...

Currently I got this error message: Fatal error: Uncaught exception 'Zend\View\Exception\RuntimeException' with message 'Zend\View\Renderer\PhpRenderer::render: Unable to render template "application/rest/get-list"; resolver could not resolve to a file' in C:\xampp\htdocs\EhcServer\vendor\zendframework\zendframework\library\Zend\View\Renderer\PhpRenderer.php on line 499

Thanks in advance!

2

2 Answers

5
votes

Your strategies need to be inside view_manager in module.config.php

ie., the view manager section should look like this

'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
votes

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,