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

Add default response header support #211

Merged
merged 1 commit into from Oct 16, 2018
Merged

Add default response header support #211

merged 1 commit into from Oct 16, 2018

Conversation

shadowhand
Copy link
Member

Refs #205

$response->getBody()->write($body);
}

if ($response instanceof ResponseInterface && ! $response->hasHeader('content-type')) {
$response = $response->withAddedHeader('content-type', 'application/json');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used withHeader instead of withAddedHeader because all of hasHeader checks, which invalidate the need to use withAddedHeader.

@@ -39,14 +41,11 @@ public function invokeRouteCallable(Route $route, ServerRequestInterface $reques

if ($this->isJsonEncodable($response)) {
$body = json_encode($response);
$response = $this->responseFactory->createResponse();
$response = $response->withStatus(200);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this, as the default for createResponse is a response with HTTP 200 status. I still made the status explicit for clarity.

Also, I did not modify JsonStrategy::getExceptionHandler response handling, as there is no assurance that the body would conform to a custom JSON format like JSON-API or HAL. Since the body defined for an exception is explicit, it would likely cause issues to apply the default headers.

@philipobenito philipobenito merged commit 4986805 into thephpleague:master Oct 16, 2018
@shadowhand shadowhand deleted the feature/extend-strategies branch October 16, 2018 14:57
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.

None yet

2 participants