Skip to content

Commit

Permalink
feature #54678 [FrameworkBundle] Add support for setting headers wi…
Browse files Browse the repository at this point in the history
…th `TemplateController` (HypeMC)

This PR was merged into the 7.2 branch.

Discussion
----------

[FrameworkBundle] Add support for setting `headers` with `TemplateController`

| Q             | A
| ------------- | ---
| Branch?       | 7.2
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Issues        | -
| License       | MIT

It would be nice to be able to set some additional headers such as `Content-Type` when using the `TemplateController`.

Commits
-------

5d41f8a068 [FrameworkBundle] Add support for setting `headers` with `TemplateController`
  • Loading branch information
fabpot committed May 21, 2024
2 parents e8cba21 + 8db2848 commit 055f3c1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.2
---

* Add support for setting `headers` with `Symfony\Bundle\FrameworkBundle\Controller\TemplateController`

7.1
---

Expand Down
7 changes: 6 additions & 1 deletion Controller/TemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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".');
Expand All @@ -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;
}

Expand Down
14 changes: 14 additions & 0 deletions Tests/Controller/TemplateControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '<svg></svg>');

$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'));
}
}

0 comments on commit 055f3c1

Please sign in to comment.