Skip to content

Commit

Permalink
feature #24637 [FrameworkBundle] Improve the DX of TemplateController…
Browse files Browse the repository at this point in the history
… when using SF 4 (dunglas)

This PR was merged into the 4.1-dev branch.

Discussion
----------

[FrameworkBundle] Improve the DX of TemplateController when using SF 4

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md files -->
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

Tiny DX improvement when using modern Symfony.

Allow to write:

```yaml
# config/routes.yaml
index:
    path: /
    defaults:
      _controller: 'Symfony\Bundle\FrameworkBundle\Controller\TemplateController'
      template: 'homepage.html.twig'
```

Instead of:

```yaml
index:
    path: /
    defaults:
      _controller: 'Symfony\Bundle\FrameworkBundle\Controller\TemplateController::templateAction'
      template: 'homepage.html.twig'
```
I was thinking about doing the same for `RedirectController`, but it's not that easy because it contains two methods.

Commits
-------

6d15055 [FrameworkBundle] Improve the DX of TemplateController when using SF 4
  • Loading branch information
Tobion committed Dec 4, 2017
2 parents a603ba0 + 6d15055 commit 8e7eac6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
Expand Up @@ -67,4 +67,9 @@ public function templateAction(string $template, int $maxAge = null, int $shared

return $response;
}

public function __invoke(string $template, int $maxAge = null, int $sharedAge = null, bool $private = null): Response
{
return $this->templateAction($template, $maxAge, $sharedAge, $private);
}
}
Expand Up @@ -23,21 +23,23 @@ class TemplateControllerTest extends TestCase
public function testTwig()
{
$twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock();
$twig->expects($this->once())->method('render')->willReturn('bar');
$twig->expects($this->exactly(2))->method('render')->willReturn('bar');

$controller = new TemplateController($twig);

$this->assertEquals('bar', $controller->templateAction('mytemplate')->getContent());
$this->assertEquals('bar', $controller('mytemplate')->getContent());
}

public function testTemplating()
{
$templating = $this->getMockBuilder(EngineInterface::class)->getMock();
$templating->expects($this->once())->method('render')->willReturn('bar');
$templating->expects($this->exactly(2))->method('render')->willReturn('bar');

$controller = new TemplateController(null, $templating);

$this->assertEquals('bar', $controller->templateAction('mytemplate')->getContent());
$this->assertEquals('bar', $controller('mytemplate')->getContent());
}

/**
Expand All @@ -49,5 +51,6 @@ public function testNoTwigNorTemplating()
$controller = new TemplateController();

$controller->templateAction('mytemplate')->getContent();
$controller('mytemplate')->getContent();
}
}

0 comments on commit 8e7eac6

Please sign in to comment.