@@ -335,7 +335,7 @@ class Response
335
335
protected $ _headers = [];
336
336
337
337
/**
338
- * Buffer string for response message
338
+ * Buffer string or callable for response message
339
339
*
340
340
* @var string
341
341
*/
@@ -538,13 +538,18 @@ protected function _sendHeader($name, $value = null)
538
538
539
539
/**
540
540
* Sends a content string to the client.
541
+ * If the content is a callable, it is invoked.
541
542
*
542
543
* @param string $content string to send as response body
543
544
* @return void
544
545
*/
545
546
protected function _sendContent ($ content )
546
547
{
547
- echo $ content ;
548
+ if (!is_string ($ content ) && is_callable ($ content )) {
549
+ call_user_func ($ content );
550
+ } else {
551
+ echo $ content ;
552
+ }
548
553
}
549
554
550
555
/**
@@ -629,7 +634,7 @@ public function location($url = null)
629
634
* Buffers the response message to be sent
630
635
* if $content is null the current buffer is returned
631
636
*
632
- * @param string|null $content the string message to be sent
637
+ * @param string|callable| null $content the string or callable message to be sent
633
638
* @return string Current message buffer if $content param is passed as null
634
639
*/
635
640
public function body ($ content = null )
@@ -1228,11 +1233,15 @@ public function checkNotModified(Request $request)
1228
1233
/**
1229
1234
* String conversion. Fetches the response body as a string.
1230
1235
* Does *not* send headers.
1236
+ * If body is a callable, a blank string is returned.
1231
1237
*
1232
1238
* @return string
1233
1239
*/
1234
1240
public function __toString ()
1235
1241
{
1242
+ if (!is_string ($ this ->_body ) && is_callable ($ this ->_body )) {
1243
+ return '' ;
1244
+ }
1236
1245
return (string )$ this ->_body ;
1237
1246
}
1238
1247
0 commit comments