Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

268
Vistas
ZF3: How to send a JSON from an action function

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.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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).

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda