Commit 36abd018 authored by Fabien Potencier's avatar Fabien Potencier

added support for sub-requests

parent add25faa
......@@ -423,6 +423,8 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
{
$this->beforeDispatched = false;
$current = HttpKernelInterface::SUB_REQUEST === $type ? $this['request'] : $this['request_error'];
$this['request'] = $request;
$this['request']->setDefaultLocale($this['request.default_locale']);
......@@ -430,7 +432,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
$response = $this['kernel']->handle($request, $type, $catch);
$this['request'] = $this['request_error'];
$this['request'] = $current;
return $response;
}
......
......@@ -332,6 +332,40 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
$request = $app['request'];
}
public function testSubRequest()
{
$app = new Application();
$app->get('/sub', function (Request $request) {
return new Response('foo');
});
$app->get('/', function (Request $request) use ($app) {
return $app->handle(Request::create('/sub'), HttpKernelInterface::SUB_REQUEST);
});
$this->assertEquals('foo', $app->handle(Request::create('/'))->getContent());
}
public function testSubRequestDoesNotReplaceMainRequestAfterHandling()
{
$mainRequest = Request::create('/');
$subRequest = Request::create('/sub');
$app = new Application();
$app->get('/sub', function (Request $request) {
return new Response('foo');
});
$app->get('/', function (Request $request) use ($subRequest, $app) {
$response = $app->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
// request in app must be the main request here
$response->setContent($response->getContent().' '.$app['request']->getPathInfo());
return $response;
});
$this->assertEquals('foo /', $app->handle($mainRequest)->getContent());
}
}
class FooController
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment