Skip to content

Instantly share code, notes, and snippets.

@asgrim
Last active March 25, 2017 12:44
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asgrim/0d8b1cb7a4fa362db8fed06444d104ae to your computer and use it in GitHub Desktop.
Save asgrim/0d8b1cb7a4fa362db8fed06444d104ae to your computer and use it in GitHub Desktop.
<?php
// Note we have no $response parameter - it discourages trying to change it on the way "in" the pipe
$a = function a($request, $next) {
$realResponse = $next($request->withAttribute('yay', 'woo'));
return $realResponse->withHeader('X-Foo', 'foo');
}
$b = function b($request, $next) {
$yay = $request->getAttribute('yay'); // woo
return new Response();
}
echo $a(new Request(), $b); // Header is set correctly here \o/
<?php
$a = function a($request, $response, $next = null) {
$realResponse = $next($request->withAttribute('yay', 'woo'), $response);
return $realResponse->withHeader('X-Foo', 'foo');
}
$b = function b($request, $response, $next = null) {
$yay = $request->getAttribute('yay'); // woo
return new Response();
}
echo $a(new Request(), new Response(), $b); // Header is set correctly here \o/
<?php
$a = function a($request, $response, $next = null) {
$response = $response->withHeader('X-Foo', 'foo'); // Setting a header beforehand... works right?... no.
return $next($request->withAttribute('yay', 'woo'), $response);
}
$b = function b($request, $response, $next = null) {
$yay = $request->getAttribute('yay'); // woo
return new Response();
}
echo $a(new Request(), new Response(), $b); // Header would be missing, because the response returned is a SEPARATE instance entirely
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment