Skip to content

Commit e2a23f8

Browse files
committed
Send response headers manually
1 parent 3bc5b65 commit e2a23f8

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

sdk/php/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ use starfederation\datastar\enums\EventType;
3030
use starfederation\datastar\enums\FragmentMergeMode;
3131
use starfederation\datastar\ServerSentEventGenerator;
3232

33-
// If your framework has its own way of sending response headers, you must manually send the headers returned by `ServerSentEventGenerator::headers()` before creating a new instance of `ServerSentEventGenerator`.
34-
3533
// Creates a new `ServerSentEventGenerator` instance.
3634
$sse = new ServerSentEventGenerator();
3735

36+
// Sends the response headers.
37+
// If your framework has its own way of sending response headers, manually send the headers returned by `ServerSentEventGenerator::headers()` instead.
38+
$sse->sendHeaders();
39+
3840
// Merges HTML fragments into the DOM.
3941
$sse->mergeFragments('<div></div>', [
4042
'selector' => '#my-div',

sdk/php/src/ServerSentEventGenerator.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,19 @@ public function __construct()
5454
{
5555
// Abort the process if the client closes the connection.
5656
ignore_user_abort(false);
57+
}
58+
59+
/**
60+
* Sends the response headers, if not already sent.
61+
*/
62+
public function sendHeaders(): void
63+
{
64+
if (headers_sent()) {
65+
return;
66+
}
5767

58-
// Sets the response headers only if not already sent and not yet set.
59-
if (!headers_sent()) {
60-
foreach (static::headers() as $name => $value) {
61-
header("$name: $value", false);
62-
}
68+
foreach (static::headers() as $name => $value) {
69+
header("$name: $value");
6370
}
6471
}
6572

0 commit comments

Comments
 (0)