Skip to content

Conversation

@pierresh
Copy link

With FlightPHP v2, we could send JSON easily, even if the method start() was not called yet.

Example:

require_once __DIR__ . '/vendor/autoload.php';

Flight::json("Hello");

Now, with v3, nothing happens unless the framework is started with Flight::start() (and it seems requiring router defined, otherwise it returns a 404 error).

This pull request aims to restore the simple behavior of Flight v2 to send a JSON.

@pierresh
Copy link
Author

I see that one unit test fails, I will try to solve it.

@n0nag0n
Copy link
Collaborator

n0nag0n commented May 2, 2025

So if you want you have a couple options.

https://docs.flightphp.com/en/v3/learn/migrating-to-v3#output-buffering-behavior-3-5-0

You can turn on the old v2 behavior to have it do what you want.

Or you can override the json method to do your bidding or create a new function altogether

https://docs.flightphp.com/en/v3/learn/migrating-to-v3#output-buffering-behavior-3-5-0

// or 'jsonOutputNow'
Flight::map('json', function($data, $status, $flags) {
    header('Content-Type: application/json');
    http_response_code($status);
    echo json_encode($data, $flags);
    exit;
});

@pierresh
Copy link
Author

pierresh commented May 2, 2025

Yes, I know, but I feel a bit strange that nothing is sent with Flight::json() in such situation. And I figured out that we have a few customized files which run outside of Flight::start() / Flight::route() for various reasons, thus changing from v2 to v3 is a breaking change.

But it is not a problem if you disagree, we will deal with them one by one.

@krmu krmu requested a review from n0nag0n July 20, 2025 10:20
@n0nag0n
Copy link
Collaborator

n0nag0n commented Jul 20, 2025

Thanks @krmu for hitting me up on this. Forgot it was still open. I'm thinking through it and I still can't understand why you would want Flight just generate and echo the JSON in a given situation, when you can just create your own function to echo out JSON. The Flight::json() method sets status codes and headers that ultimately will never get sent to the browser. Maybe it is ill named and that's the confusion? I can see that people may think that this is how you use the framework to encode JSON when it should be something more like a flight\util\Json class that encodes the JSON for you. Then you could use that function to encode the JSON and spit it out if you wanted to.......I actually like that idea better and it would solve this PR and there could be other use cases for it.

@n0nag0n
Copy link
Collaborator

n0nag0n commented Jul 20, 2025

Replaced with #657

@n0nag0n n0nag0n closed this Jul 20, 2025
@n0nag0n
Copy link
Collaborator

n0nag0n commented Jul 20, 2025

You can now use echo Json::encode($myVar) to do what you want and it will keep the default flags that the Flight Framework uses to keep your code consistent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants