Skip to content

Commit 09089bc

Browse files
seb-jeanjrushlow
andauthored
feature #1583 [make:crud] Remove / from from index action URL
* Update Controller.tpl.php * Update it_generates_basic_crud.php * allow empty path arguments --------- Co-authored-by: Jesse Rushlow <jr@rushlow.dev>
1 parent 207ed9c commit 09089bc

7 files changed

+15
-7
lines changed

src/Resources/skeleton/crud/controller/Controller.tpl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#[Route('<?= $route_path ?>')]
88
<?= $class_data->getClassDeclaration() ?>
99
{
10-
<?= $generator->generateRouteForControllerMethod('/', sprintf('%s_index', $route_name), ['GET']) ?>
10+
<?= $generator->generateRouteForControllerMethod('', sprintf('%s_index', $route_name), ['GET']) ?>
1111
<?php if (isset($repository_full_class_name)): ?>
1212
public function index(<?= $repository_class_name ?> $<?= $repository_var ?>): Response
1313
{

src/Resources/skeleton/crud/test/Test.EntityManager.tpl.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ protected function setUp(): void
2727

2828
public function testIndex(): void
2929
{
30+
$this->client->followRedirects();
3031
$crawler = $this->client->request('GET', $this->path);
3132

3233
self::assertResponseStatusCodeSame(200);

src/Util/TemplateComponentGenerator.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,16 @@ public function __construct(
2727
) {
2828
}
2929

30-
public function generateRouteForControllerMethod(string $routePath, string $routeName, array $methods = [], bool $indent = true, bool $trailingNewLine = true): string
30+
/**
31+
* @param string|null $routePath passing an empty string/null will create a route attribute without the "path" argument
32+
*/
33+
public function generateRouteForControllerMethod(?string $routePath, string $routeName, array $methods = [], bool $indent = true, bool $trailingNewLine = true): string
3134
{
32-
$attribute = \sprintf('%s#[Route(\'%s\', name: \'%s\'', $indent ? ' ' : null, $routePath, $routeName);
35+
if (!empty($routePath)) {
36+
$path = \sprintf('\'%s\', ', $routePath);
37+
}
38+
39+
$attribute = \sprintf('%s#[Route(%sname: \'%s\'', $indent ? ' ' : null, $path ?? null, $routeName);
3340

3441
if (!empty($methods)) {
3542
$attribute .= ', methods: [';

tests/fixtures/make-crud/expected/WithCustomRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#[Route('/sweet/food')]
1515
final class SweetFoodController extends AbstractController
1616
{
17-
#[Route('/', name: 'app_sweet_food_index', methods: ['GET'])]
17+
#[Route(name: 'app_sweet_food_index', methods: ['GET'])]
1818
public function index(SweetFoodRepository $sweetFoodRepository): Response
1919
{
2020
return $this->render('sweet_food/index.html.twig', [

tests/fixtures/make-crud/tests/it_generates_basic_crud.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class GeneratedCrudControllerTest extends WebTestCase
99
public function testIndexAction()
1010
{
1111
$client = self::createClient();
12-
$crawler = $client->request('GET', '/sweet/food/');
12+
$crawler = $client->request('GET', '/sweet/food');
1313
$this->assertTrue($client->getResponse()->isSuccessful());
1414
$this->assertStringContainsString('<!DOCTYPE html>', $client->getResponse()->getContent());
1515
$this->assertStringContainsString('SweetFood index', $client->getResponse()->getContent());

tests/fixtures/make-crud/tests/it_generates_crud_with_custom_controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class GeneratedCrudControllerTest extends WebTestCase
99
public function testIndexAction()
1010
{
1111
$client = self::createClient();
12-
$crawler = $client->request('GET', '/sweet/food/admin/');
12+
$crawler = $client->request('GET', '/sweet/food/admin');
1313
$this->assertTrue($client->getResponse()->isSuccessful());
1414
$this->assertStringContainsString('<!DOCTYPE html>', $client->getResponse()->getContent());
1515
$this->assertStringContainsString('SweetFood index', $client->getResponse()->getContent());

tests/fixtures/make-crud/tests/it_generates_crud_with_custom_root_namespace.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class GeneratedCrudControllerTest extends WebTestCase
99
public function testIndexAction()
1010
{
1111
$client = self::createClient();
12-
$crawler = $client->request('GET', '/sweet/food/');
12+
$crawler = $client->request('GET', '/sweet/food');
1313
$this->assertTrue($client->getResponse()->isSuccessful());
1414
$this->assertStringContainsString('<!DOCTYPE html>', $client->getResponse()->getContent());
1515
$this->assertStringContainsString('SweetFood index', $client->getResponse()->getContent());

0 commit comments

Comments
 (0)