From 5d41f8a0685a6fc71e5fbb110a9f8a2a9ab632c3 Mon Sep 17 00:00:00 2001 From: HypeMC Date: Fri, 19 Apr 2024 20:47:43 +0200 Subject: [PATCH] [FrameworkBundle] Add support for setting `headers` with `TemplateController` --- src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md | 5 +++++ .../Controller/TemplateController.php | 7 ++++++- .../Tests/Controller/TemplateControllerTest.php | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index df0d692ebdf7a..75ffef1a60386 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +7.2 +--- + + * Add support for setting `headers` with `Symfony\Bundle\FrameworkBundle\Controller\TemplateController` + 7.1 --- diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/TemplateController.php b/src/Symfony/Bundle/FrameworkBundle/Controller/TemplateController.php index bcbcc382d7f64..125e04f62aab1 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/TemplateController.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/TemplateController.php @@ -37,8 +37,9 @@ public function __construct( * @param bool|null $private Whether or not caching should apply for client caches only * @param array $context The context (arguments) of the template * @param int $statusCode The HTTP status code to return with the response (200 "OK" by default) + * @param array $headers The HTTP headers to add to the response */ - public function templateAction(string $template, ?int $maxAge = null, ?int $sharedAge = null, ?bool $private = null, array $context = [], int $statusCode = 200): Response + public function templateAction(string $template, ?int $maxAge = null, ?int $sharedAge = null, ?bool $private = null, array $context = [], int $statusCode = 200, array $headers = []): Response { if (null === $this->twig) { throw new \LogicException('You cannot use the TemplateController if the Twig Bundle is not available. Try running "composer require symfony/twig-bundle".'); @@ -60,6 +61,10 @@ public function templateAction(string $template, ?int $maxAge = null, ?int $shar $response->setPublic(); } + foreach ($headers as $key => $value) { + $response->headers->set($key, $value); + } + return $response; } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/TemplateControllerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/TemplateControllerTest.php index c972151d2c0b0..28f63ac017a5b 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/TemplateControllerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/TemplateControllerTest.php @@ -84,4 +84,18 @@ public function testStatusCode() $this->assertSame(201, $controller->templateAction($templateName, null, null, null, [], $statusCode)->getStatusCode()); $this->assertSame(200, $controller->templateAction($templateName)->getStatusCode()); } + + public function testHeaders() + { + $templateName = 'image.svg.twig'; + + $loader = new ArrayLoader(); + $loader->setTemplate($templateName, ''); + + $twig = new Environment($loader); + $controller = new TemplateController($twig); + + $this->assertSame('image/svg+xml', $controller->templateAction($templateName, headers: ['Content-Type' => 'image/svg+xml'])->headers->get('Content-Type')); + $this->assertNull($controller->templateAction($templateName)->headers->get('Content-Type')); + } }