Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

266
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!