cakephp: 3.4 Regression on Cake\Http\Response::withBody(string)
This is a (multiple allowed):
-
bug
-
enhancement
-
feature-discussion (RFC)
-
CakePHP Version:3.4.2.
Context
Trying to upgrade CakePHP 3.3 project to 3.4 using new immutable Request/Response objects: https://book.cakephp.org/3.0/en/appendices/3-4-migration-guide.html
In CakePHP 3.3 controller:
public function index()
{
$this->response->disableCache();
$this->response->type('application/json');
$this->response->body(json_encode('TEST'));
return $this->response;
}
Result: OK - JSON response with string.
In CakePHP 3.4 controller:
public function index()
{
return $this->response
->withDisabledCache()
->withType('application/json')
->withBody(json_encode('TEST'));
}
Result: Fatal error
Fatal Error
Cake\Error\FatalErrorException
Argument 1 passed to Cake\Http\Response::withBody() must implement interface Psr\Http\Message\StreamInterface, string given
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 17 (12 by maintainers)
I can see how the migration guide’s terseness was misleading. In this situation I would prefer to update the documentation over adding more methods. For this specific scenario you could also use
Which feels like a non-obtrusive amount of code to write.
Thank you for the quick fix but I stand by my report that this is an issue.
Migration guideline says:
Since it was working with CakePHP 3.3, it should be working with 3.4 or be properly documented.
Also, objectively compare the ease of use in CakePHP 3.3:
vs CakePHP 3.4:
My motivation behind this report is to keep CakePHP rapid development friendly. Small things matter.
I Iike the idea of adding more convenience methods. I would name it
withStringBody()thoughThank you @ADmad.
How about option 2? There is a
withFile($path, $options)already that does something similar for files.Could it be something like
withString($content, $type)?I’m getting the feeling that once it’s documented this issue won’t be solved.
So, please consider:
If Body is a component of Response class (it is logical reasoning from developer standpoint - I am the caller, I’m operating on Response class - not on inner Body class), then whether Body is immutable or not, the implementation details should be contained within withBody() method and I shouldn’t be concerned about inner details.
It was like that in CakePHP 3.3:
The reason why this is so important is that this class will be used everywhere to generate HTTP responses and current usage is just bad.
Ask yourself - should this much code be needed for rapid development framework to just generate ‘HELLO WORLD’ without rendering a view?
In my case, I’m using CakePHP as a HTTP framework to proxy some business services using standard Cake models and there is a lot of code that just renders responses (web services).
I always can ease myself and create helper method somewhere but I would rather get to the source and fix it there so other people won’t have to stumble onto this.
So, regarding possible solutions and an open discussion (I hope).
In CakePHP 3.4 there is:
To fix this I’m proposing:
I’m long time CakePHP user (since 1.1) and a big believer into project mission so if I can help on this - please advice.
You are absolutely right, we should improve the migration docs here. Are you able to provide a PR to clarify things? That would be awesome!