We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Problem with returning Json string

Hi, can anyone explain that problem:

private function handleApiCall($dispatcher)
{
    $xyz = $this->view->getParamsToView();

    $this->view->disableLevel(array(
        View::LEVEL_ACTION_VIEW => true,
        View::LEVEL_LAYOUT => true,
        View::LEVEL_MAIN_LAYOUT => true,
        View::LEVEL_AFTER_TEMPLATE => true,
        View::LEVEL_BEFORE_TEMPLATE => true
    ));

    unset($xyz['_user']);
    unset($xyz['lang']);
    unset($xyz['_filter']);

    var_dump($xyz);

    die;

    $this->response->setJsonContent($xyz);

}

result with that:

array (size=2) 'boolean' => boolean true 'error' => array (size=2) 0 => string 'a' (length=1) 1 => string 'b' (length=1)

but when I remove die; statement I'm getting:

array (size=0) empty []

so some magic happend and $xyz variable with array was cleared :/

Here is an example of my rest api response:

public function afterExecuteRoute()
{
    // Status code & Response header 
    $this->response->setStatusCode($this->_response['statusCode'], $this->_statusCodes[$this->_response['statusCode']]);
    $this->response->setHeader('Access-Control-Allow-Origin', '*');
    $this->response->setHeader('X-Content-Type-Options', 'nosniff');
    $this->response->setHeader('X-Frame-Options', 'deny');
    $this->response->setHeader('Content-Security-Policy', 'default-src \'none\'');
    // header_remove('x-powered-by');
    // header_remove('server');

    // Allow user to choose content type. Defaults to JSON
    if ($this->request->getHeader('Accept') == 'application/xml') {
        $this->response->setContentType('application/xml', 'UTF-8'); 
        $this->response->setContent(\Helpers\ArrayToXml::convert($this->_response));
    } else {
        $this->response->setContentType('application/json', 'UTF-8');
        $this->response->setJsonContent($this->_response); 
    }

    // Log
    // ...

    // Return the response
    return $this->response->send();
}
edited Nov '18

Your handleApiCall method is private so I imagine it isn't the one being called via a route. What is the calling method in that class doing? Are you sure it's not adjusting the response in any way or returning any other value?

What I have is a middleware class that checks to see if the controller method (or route) is returning an instance of the Response class or not. If not, then it converts the value to json and returns. It looks something like:

class JsonResponseMiddleware implements MiddlewareInterface
{
    /**
     * After route has finished executing, do this
     *
     * @param Event $event
     * @param Micro $application
     * @returns bool
     */
    public function afterHandleRoute(Event $event, Micro $application)
    {
        $returned = $application->getReturnedValue();
        if (!($returned instanceof Response)) {
            $application->response
                ->setJsonContent($returned)
                ->send();
        } else {
            $returned->send();
        }
        return true;
    }

    /**
     * Calls the middleware
     *
     * @param Micro $application
     * @returns bool
     */
    public function call(Micro $application)
    {
        return true;
    }
}

Then from the code that handles the route I could simply return the value. Taking your code as an example:

public function handleApiCall()
{
    $xyz = $this->view->getParamsToView();
    unset($xyz['_user'], $xyz['lang'], $xyz['_filter']);
    return $xyz;
}

But check to see what your calling function is doing with the response - maybe that's where it's going wrong.