Skip to content

Commit

Permalink
Merge pull request #1120 from l-vo/return_json_response_when_json_met…
Browse files Browse the repository at this point in the history
…hod_used

[make:controller] Return a JsonResponse instead of a Response with --no-template
  • Loading branch information
jrushlow committed May 16, 2022
2 parents fc9f6bc + 4aebedf commit 23d71ff
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/Maker/MakeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\Annotation\Route;
Expand Down Expand Up @@ -73,13 +74,14 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
'Controller'
);

$withTemplate = $this->isTwigInstalled() && !$input->getOption('no-template');

$useStatements = new UseStatementGenerator([
AbstractController::class,
Response::class,
$withTemplate ? Response::class : JsonResponse::class,
Route::class,
]);

$noTemplate = $input->getOption('no-template');
$templateName = Str::asFilePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()).'/index.html.twig';
$controllerPath = $generator->generateController(
$controllerClassNameDetails->getFullName(),
Expand All @@ -88,12 +90,12 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
'use_statements' => $useStatements,
'route_path' => Str::asRoutePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
'route_name' => Str::asRouteName($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
'with_template' => $this->isTwigInstalled() && !$noTemplate,
'with_template' => $withTemplate,
'template_name' => $templateName,
]
);

if ($this->isTwigInstalled() && !$noTemplate) {
if ($withTemplate) {
$generator->generateTemplate(
$templateName,
'controller/twig_template.tpl.php',
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/skeleton/controller/Controller.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
class <?= $class_name; ?> extends AbstractController
{
<?= $generator->generateRouteForControllerMethod($route_path, $route_name); ?>
public function index(): Response
public function index(): <?php if ($with_template) { ?>Response<?php } else { ?>JsonResponse<?php } ?>

{
<?php if ($with_template) { ?>
return $this->render('<?= $template_name ?>', [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public function testController()
$client->request('GET', '/foo/bar');

$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type'));
$this->assertSame('{"message":"Welcome to your new controller!","path":"src\/Controller\/FooBarController.php"}', $client->getResponse()->getContent());
}
}

0 comments on commit 23d71ff

Please sign in to comment.