Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitee6bbb2

Browse files
committed
feature#41414 SupportstatusCode default param when loading template directly via route (dayallnash)
This PR was merged into the 5.4 branch.Discussion----------Support `statusCode` default param when loading template directly via route_This is my first PR to Symfony, so please be patient as I get to grips with the 'admin' process of getting everything exactly how you want it!_| Q | A| ------------- | ---| Branch? | 5.4| Bug fix? | no| New feature? | yes| Deprecations? | no| Tickets || License | MIT| Doc PR |symfony/symfony-docs#15376### TODO- [x] submit changes to the documentation and update this PR with a link### SummaryAdded support for `statusCode` default parameter when loading a template directly from route via the `Symfony\Bundle\FrameworkBundle\Controller\TemplateController` controller (like [this](https://symfony.com/doc/current/templates.html#templates-render-from-route)). This will continue to default to a 200 code, but can be changed by updating your route - for instance something like this:```test_route: path: /test_route controller: Symfony\Bundle\FrameworkBundle\Controller\TemplateController defaults: # the path of the template to render template: 'test.html.twig' # the status code to include in the response headers statusCode: 201```This could be useful for when you want to render a template without adding any extra business logic in a controller, but don't want to return a 200 response.Commits-------5a7b666 Added support for `statusCode` default parameter when loading a template directly from route using the `Symfony\Bundle\FrameworkBundle\Controller\TemplateController` controller.
2 parents2771d6e +5a7b666 commitee6bbb2

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

‎src/Symfony/Bundle/FrameworkBundle/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
---

‎src/Symfony/Bundle/FrameworkBundle/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-
publicfunctiontemplateAction(string$template,int$maxAge =null,int$sharedAge =null,bool$private =null,array$context = []):Response
43+
publicfunctiontemplateAction(string$template,int$maxAge =null,int$sharedAge =null,bool$private =null,array$context = [],int$statusCode =200):Response
4344
{
4445
if (null ===$this->twig) {
4546
thrownew \LogicException('You cannot use the TemplateController if the Twig Bundle is not available.');
4647
}
4748

48-
$response =newResponse($this->twig->render($template,$context));
49+
$response =newResponse($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-
publicfunction__invoke(string$template,int$maxAge =null,int$sharedAge =null,bool$private =null,array$context = []):Response
68+
publicfunction__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
}

‎src/Symfony/Bundle/FrameworkBundle/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+
publicfunctiontestStatusCode()
64+
{
65+
$templateName ='template_controller.html.twig';
66+
$statusCode =201;
67+
68+
$loader =newArrayLoader();
69+
$loader->setTemplate($templateName,'<h1>{{param}}</h1>');
70+
71+
$twig =newEnvironment($loader);
72+
$controller =newTemplateController($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)

[8]ページ先頭

©2009-2025 Movatter.jp