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

$app->notFound response json issues;

I am using Micro Application to develop API, the first code is correct:

$app->notFound(function () use ($app) {
    $app->response->setStatusCode(404, "Not Found")->sendHeaders();
    echo ERROR_C05;
});

but when i use json response:

$app->notFound(function () use ($app) {
    $response = new \Phalcon\Http\Response();
    $response->setContentType('application/json', 'utf-8');
    $response->setContent(json_encode(ERROR_C05, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
    $response->setStatusCode(404, "Not Found")->sendHeaders();
    return $response;
});

The page is nothing to reply, I promise the json syntax is correct as I am using it to handle API's valid request. I think this is also bugs??? why response is not the same expectation???



2.6k

Aren't the headers sent twice?

You are sending the headers manually and returning a Reponse object. When you return a Response object its send() method is called and the headers and body are send.

Does removing ->sendHeaders() help?



18.7k

The page shows a white page without any error messages and i have remove ->sendHeaders() now;

$response = new \Phalcon\Http\Response();
$response->setStatusCode(404, "Not Found");
$response->setContentType('application/json', 'utf-8');
$response->setContent(json_encode(ERROR_C05, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
return $response;


2.6k
Accepted
answer

I've just checked the 2.0 source code. A response is only returned on a normal request, not on an not found handler. The solution would be to not return a response object from your handler but instead send it yourself.

$reponse->send(); instead of return $response;

This is what happens when a normal reponse is returned:

/**
 * Check if the returned object is already a response
 */
if typeof returnedValue == "object" {
    if returnedValue instanceof \Phalcon\Http\ResponseInterface {
        /**
         * Automatically send the response
         */
        returnedValue->send();
    }
}

But this is what happens when the NotFoundHandler is called:

/**
 * Call the Not-Found handler
 */
let returnedValue = call_user_func(notFoundHandler);

/**
 * Update the returned value
 */
let this->_returnedValue = returnedValue;

return returnedValue;


18.7k

$reponse->send(); instead of return $response; is ok now! thanks for your support!

$app->notFound(function () use ($app) {

    //$app->response->setStatusCode(404, "Not Found")->sendHeaders();
    //echo ERROR_C05;

    $message = array();
    array_push($message, array('C05', ERROR_C05));

    $response = new \Phalcon\Http\Response();
    $response->setContentType('application/json', 'utf-8');
    $response->setStatusCode(404, "Not Found")->sendHeaders();
    $response->setContent(json_encode($message, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
    $response->send();
});


18.7k
edited Jun '14

And could you help me on my topic, which is using Phalcon message queue with Beanstalkd, i do not know how to use it, even i see official document! https://forum.phalcon.io/discussion/2482/phalcon-and-beanstalkd-how-to-use-it-



2.6k

I've replied to your topic. Also don't forget to mark the reaction as answer so ohters can see the question has been answered.