Skip to content

Commit d14c95b

Browse files
authored
Merge pull request #84 from juherr/master
2 parents 9374b67 + 4d4afd5 commit d14c95b

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/ControllerInvoker.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __invoke(
3636
): ResponseInterface {
3737
// Inject the request and response by parameter name
3838
$parameters = [
39-
'request' => $request,
39+
'request' => self::injectRouteArguments($request, $routeArguments),
4040
'response' => $response,
4141
];
4242
// Inject the route arguments by name
@@ -46,4 +46,13 @@ public function __invoke(
4646

4747
return $this->invoker->call($callable, $parameters);
4848
}
49+
50+
private static function injectRouteArguments(ServerRequestInterface $request, array $routeArguments): ServerRequestInterface
51+
{
52+
$requestWithArgs = $request;
53+
foreach ($routeArguments as $key => $value) {
54+
$requestWithArgs = $requestWithArgs->withAttribute($key, $value);
55+
}
56+
return $requestWithArgs;
57+
}
4958
}

tests/RoutingTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ public function injects_route_placeholder()
4444
$this->assertEquals('Hello matt', (string) $response->getBody());
4545
}
4646

47+
/**
48+
* @test
49+
*/
50+
public function injects_route_placeholder_from_request()
51+
{
52+
$app = Bridge::create();
53+
$app->get('/{name}', function ($request, $response) {
54+
$response->getBody()->write('Hello ' . $request->getAttribute('name'));
55+
return $response;
56+
});
57+
58+
$response = $app->handle(RequestFactory::create('/matt'));
59+
$this->assertEquals('Hello matt', (string) $response->getBody());
60+
}
61+
4762
/**
4863
* @test
4964
*/

0 commit comments

Comments
 (0)