0
votes

I have a problem when I want to send a json object instead of a view from an action function.

I have configured my /config/modules.config.php like this ...

return [
    //...

    'view_manager' => [
        //...

        'strategies' => [
            'ViewJsonStrategy',
        ],
    ],
];

And when I try to return a JSON Object from my action function ...

public function loginAction(){
    $request = $this->getRequest();
    $log = new \File\LogWriter();
    $log->writeLog(get_class($this) . "::" . __FUNCTION__ . ": Dentro de loginAction()");

    $params = json_decode(file_get_contents('php://input'),true);
    $email = $params["email"];
    $password = $params["password"];

    $log->writeLog(get_class($this) . "::" . __FUNCTION__ . ": email: " . $email . " password: " . $password);

    return new JsonModel([
        "result"    => 0
    ]);             
}

I've got this error ...

Fatal error: Uncaught Zend\View\Exception\RuntimeException: Zend\View\Renderer\PhpRenderer::render: Unable to render template "application/login/login"; resolver could not resolve to a file in /var/www/html/31juegos/vendor/zendframework/zend-view/src/Renderer/PhpRenderer.php:494

Stack trace: #0 /var/www/html/31juegos/vendor/zendframework/zend-view/src/View.php(207): Zend\View\Renderer\PhpRenderer->render() #1 /var/www/html/31juegos/vendor/zendframework/zend-mvc/src/View/Http/DefaultRenderingStrategy.php(105): Zend\View\View->render(Object(Zend\View\Model\JsonModel))

#2 /var/www/html/31juegos/vendor/zendframework/zend-eventmanager/src/EventManager.php(322): Zend\Mvc\View\Http\DefaultRenderingStrategy->render(Object(Zend\Mvc\MvcEvent))

#3 /var/www/html/31juegos/vendor/zendframework/zend-eventmanager/src/EventManager.php(171): Zend\EventManager\EventManager->triggerListeners(Object(Zend\Mvc\MvcEvent))

#4 /var/www/html/31juegos/vendor/zendframework/zend-mvc/src/View/Http/DefaultRenderingStrategy.php(123): Zend\EventManager\Ev in /var/www/html/31juegos/vendor/zendframework/zend-view/src/Renderer/PhpRenderer.php on line 494

What am I doing wrong? The same code works fine in Zend Framework 2.4. I'm using php7.0.

1

1 Answers

1
votes

When the request is as a regular webpage, then the response is a webpage (not JSON). If the request is from AJAX, then the response is JSON (and it does not need a view, which is the error you're getting).