Skip to content

Commit adfe868

Browse files
dayallnashfabpot
authored andcommitted
Added support for statusCode default parameter when loading a template directly from route using the Symfony\Bundle\FrameworkBundle\Controller\TemplateController controller.
1 parent aa5d3d0 commit adfe868

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ CHANGELOG
1717
* Add `configureContainer()`, `configureRoutes()`, `getConfigDir()` and `getBundlesPath()` to `MicroKernelTrait`
1818
* Add support for configuring log level, and status code by exception class
1919
* Bind the `default_context` parameter onto serializer's encoders and normalizers
20+
* Add support for `statusCode` default parameter when loading a template directly from route using the `Symfony\Bundle\FrameworkBundle\Controller\TemplateController` controller
2021

2122
5.3
2223
---

Controller/TemplateController.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,20 @@ public function __construct(Environment $twig = null)
3333
/**
3434
* Renders a template.
3535
*
36-
* @param string $template The template name
37-
* @param int|null $maxAge Max age for client caching
38-
* @param int|null $sharedAge Max age for shared (proxy) caching
39-
* @param bool|null $private Whether or not caching should apply for client caches only
40-
* @param array $context The context (arguments) of the template
36+
* @param string $template The template name
37+
* @param int|null $maxAge Max age for client caching
38+
* @param int|null $sharedAge Max age for shared (proxy) caching
39+
* @param bool|null $private Whether or not caching should apply for client caches only
40+
* @param array $context The context (arguments) of the template
41+
* @param int $statusCode The HTTP status code to return with the response. Defaults to 200
4142
*/
42-
public function templateAction(string $template, int $maxAge = null, int $sharedAge = null, bool $private = null, array $context = []): Response
43+
public function templateAction(string $template, int $maxAge = null, int $sharedAge = null, bool $private = null, array $context = [], int $statusCode = 200): Response
4344
{
4445
if (null === $this->twig) {
4546
throw new \LogicException('You cannot use the TemplateController if the Twig Bundle is not available.');
4647
}
4748

48-
$response = new Response($this->twig->render($template, $context));
49+
$response = new Response($this->twig->render($template, $context), $statusCode);
4950

5051
if ($maxAge) {
5152
$response->setMaxAge($maxAge);
@@ -64,8 +65,8 @@ public function templateAction(string $template, int $maxAge = null, int $shared
6465
return $response;
6566
}
6667

67-
public function __invoke(string $template, int $maxAge = null, int $sharedAge = null, bool $private = null, array $context = []): Response
68+
public function __invoke(string $template, int $maxAge = null, int $sharedAge = null, bool $private = null, array $context = [], int $statusCode = 200): Response
6869
{
69-
return $this->templateAction($template, $maxAge, $sharedAge, $private, $context);
70+
return $this->templateAction($template, $maxAge, $sharedAge, $private, $context, $statusCode);
7071
}
7172
}

Tests/Controller/TemplateControllerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,19 @@ public function testContext()
5959
$this->assertEquals($expected, $controller->templateAction($templateName, null, null, null, $context)->getContent());
6060
$this->assertEquals($expected, $controller($templateName, null, null, null, $context)->getContent());
6161
}
62+
63+
public function testStatusCode()
64+
{
65+
$templateName = 'template_controller.html.twig';
66+
$statusCode = 201;
67+
68+
$loader = new ArrayLoader();
69+
$loader->setTemplate($templateName, '<h1>{{param}}</h1>');
70+
71+
$twig = new Environment($loader);
72+
$controller = new TemplateController($twig);
73+
74+
$this->assertSame(201, $controller->templateAction($templateName, null, null, null, [], $statusCode)->getStatusCode());
75+
$this->assertSame(200, $controller->templateAction($templateName)->getStatusCode());
76+
}
6277
}

0 commit comments

Comments
 (0)