Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Suggestion) change Response::withJson() to be in line with PSR-7 methods #1776

Closed
npx opened this issue Feb 23, 2016 · 1 comment
Closed

Comments

@npx
Copy link

npx commented Feb 23, 2016

public function withJson($data, $status = 200, $encodingOptions = 0)
{
    $body = $this->getBody();
    $body->rewind();
    $body->write(json_encode($data, $encodingOptions));

    return $this->withStatus($status)->withHeader('Content-Type', 'application/json;charset=utf-8');
}

Yes, withJson() is not part of PSR-7, but the naming suggests that it works like the other PSR-7 compliant methods.

I expected $response->withStatus(500)->withJson([ ... ]) to not override my status code.

Granted, I could use it as $response->withJson([...], 500) which to be honest I did not know about until I stumbled across this.

I feel like the naming strongly suggests the usual PSR-7 "cloning and keep your properties"-behaviour and suggest keeping the status code:

public function withJson($data, $status = null, $encodingOptions = 0)
{
    $body = $this->getBody();
    $body->rewind();
    $body->write(json_encode($data, $encodingOptions));

    $status = is_null($status) ? $this->getStatusCode() : null;

    return $this->withStatus($status)->withHeader('Content-Type', 'application/json;charset=utf-8');
}

What do you think?

TL;DR withJson is not part of PSR-7 but is named like a PSR-7 compliant method and therefore should not override a response's status code

@akrabat
Copy link
Member

akrabat commented Feb 23, 2016

This has already been fixed in #1737 and will be available in version 3.2.

@akrabat akrabat closed this as completed Feb 23, 2016
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

No branches or pull requests

2 participants