Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

267
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda