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

JsonApiResponseTrait::addHeaders behaviour #30

Open
lunika opened this issue Jan 18, 2017 · 1 comment
Open

JsonApiResponseTrait::addHeaders behaviour #30

lunika opened this issue Jan 18, 2017 · 1 comment

Comments

@lunika
Copy link

lunika commented Jan 18, 2017

Hi,

I would like to understand what it is the behaviour for the method JsonApiResponseTrait::addHeaders ?

This method do nothing more than returning the response parameter and is named addHeaders but there is no header parameter, only the response. Is this method only here to be overrided ? Something else ?

@strayobject
Copy link

Yeah, you should overwrite it in your class which is implementing JsonApiResponseTrait.
Remember that the response is immutable and as such your implementation should look along these lines:

protected function addHeaders(ResponseInterface $response)
    {
        $response = $response->withHeader('x-my-header', 'some value');
        
        return $response;
    }

An alternative to that would be to add the headers you need in the controller method (this way you are using symfony's param bag, for example:

public function myAction(Request $request)
    {
        [...]
        $response = $this->response($data);
        $response->headers->add(['x-my-header' => 'some value']);

        return $response;
    }

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