1

im developing an API with FlightPHP microframework and I can't set an HTTP response code for my routes. I can set this and works perfectly:

header('HTTP/1.0 500 Error');

But I want use the native function http_response_code() from PHP. This one don't do anything. I want to use this because that I don't have to manually type the error message.

3 Answers3

3

To return a HTTP response code using Flight, you can do it like this :

Flight::route('GET /', function(){
    Flight::json($data, $code = 500);
}); 

Where $data is the variable that leads to the array you want to send in json. If $code is not set, then the default returned HTTP response code is "200". https://github.com/mikecao/flight/blob/e25f023d4377a2b99b4be8bf7977f3fc0f8089c8/flight/Engine.php#L500

Artyum
  • 103
  • 2
  • 8
1

from flight php

 Flight::json($data, [$code], [$encode], [$charset], [$option]) // Sends a JSON response.
    Flight::jsonp($data, [$param], [$code], [$encode], [$charset], [$option]) // Sends a JSONP response.
ajimsha
  • 19
  • 1
  • 4
0

I had a similar problem, to send my own headers, I do so:

$code = 404;
Flight->before('stop', function(&$params) use ($code) {
    $params[0] = $code;
});