Response->type vs response->withType

When setting a response content-type header, what is the difference between

$response->type('content/joy');

and

$response->withType('content/joy');

Other than the fact that the former seems to work and the later does not?

I’m using CakePHP 3.5.x, and the only way I can control content type for responses is to use the first method. The second method does nothing.

The first one is keeped for backwards compatibility reasons, it will be deprecated in 3.6 and removed in 4.0

The second one is part of the PSR-7 interface, it was created to be inmutable. You should use it like this

$this->response = $response->withType('content/joy');

Why in Gawd’s name would they make the response object immutable? That would make sense for the request, but the entire point of the framework is that I have control over the response.

That should be revisited, as it’s a really bad idea.

1 Like

Its part of the PHP-FIG and not CakePHP alone.
This will allow the PHP ecosystem to grow in packages that can coexists without building bridges, every modern framework/library will use the inmutable request/response