-
Notifications
You must be signed in to change notification settings - Fork 152
Add shortcut method for empty responses #58
Conversation
@franzliedke I'd argue a status 204 with a reason phrase "No Content" as defaults would make most sense here; you could then call the method |
@@ -80,12 +92,12 @@ private function __construct() | |||
* @param string $contentType | |||
* @return Response | |||
*/ | |||
private static function createResponse($body, $status, array $headers, $contentType) | |||
private static function createResponse($body, $status, array $headers, $contentType = null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep the original signature, but have the caller send a null value or an empty string.
Also, needs tests. :) |
@weierophinney Took care of your feedback: |
On review, I'm not sure this belongs in the I was commenting earlier on another PR, and I wonder if perhaps we could do this instead: namespace Zend\Diactoros\Response;
use Zend\Diactoros\Response;
use Zend\Diactoros\Stream;
class NoContent extends Response
{
public function __construct(array $headers = [], $status = 204)
{
$body = new Stream('php://memory', 'r');
parent::__construct($body, $status, $headers);
}
} With usage like: // Assuming "use Zend\Diactoros\Response\NoContent;
return new NoContent(); This provides the simplicity you're looking for, while keeping it semantically separate from the string-based response types. The constructor omits the body, and ensures the body is read-only. Since a "no content" status typically won't change, headers become the first argument, as those are the one aspect of the response that you may want to vary (e.g., to provide a location header, a link header, an authorization token, etc.). Thoughts? |
Why not. I wouldn't change the arguments order, though, that would be confusing. Also, you may want to have empty responses for something like When only wanting to change the header, but not caring about the status, we could even provide an alternate static constructor, or people could just resort to this: $response = (new NoContent)->withHeaders(...); |
@franzliedke You've convinced me; go for it! |
Can we call it |
@franzliedke sure. |
Done as you suggested. Used |
@franzliedke I'd chosen |
Ah, thanks for clarifying. I think I'm done with this one. :) |
Add shortcut method for empty responses
StringResponse::blank(401)
, as that's another common use case (though nowStringResponse
may not be perfect).It's called
blank()
becauseempty()
cannot be used as method name until PHP 7. Any better ideas?