Skip to content

Commit

Permalink
feature #1583 [make:crud] Remove / from from index action URL
Browse files Browse the repository at this point in the history
* Update Controller.tpl.php

* Update it_generates_basic_crud.php

* allow empty path arguments

---------

Co-authored-by: Jesse Rushlow <[email protected]>
  • Loading branch information
seb-jean and jrushlow authored Aug 29, 2024
1 parent 207ed9c commit 09089bc
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Resources/skeleton/crud/controller/Controller.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#[Route('<?= $route_path ?>')]
<?= $class_data->getClassDeclaration() ?>
{
<?= $generator->generateRouteForControllerMethod('/', sprintf('%s_index', $route_name), ['GET']) ?>
<?= $generator->generateRouteForControllerMethod('', sprintf('%s_index', $route_name), ['GET']) ?>
<?php if (isset($repository_full_class_name)): ?>
public function index(<?= $repository_class_name ?> $<?= $repository_var ?>): Response
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protected function setUp(): void

public function testIndex(): void
{
$this->client->followRedirects();
$crawler = $this->client->request('GET', $this->path);

self::assertResponseStatusCodeSame(200);
Expand Down
11 changes: 9 additions & 2 deletions src/Util/TemplateComponentGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ public function __construct(
) {
}

public function generateRouteForControllerMethod(string $routePath, string $routeName, array $methods = [], bool $indent = true, bool $trailingNewLine = true): string
/**
* @param string|null $routePath passing an empty string/null will create a route attribute without the "path" argument
*/
public function generateRouteForControllerMethod(?string $routePath, string $routeName, array $methods = [], bool $indent = true, bool $trailingNewLine = true): string
{
$attribute = \sprintf('%s#[Route(\'%s\', name: \'%s\'', $indent ? ' ' : null, $routePath, $routeName);
if (!empty($routePath)) {
$path = \sprintf('\'%s\', ', $routePath);
}

$attribute = \sprintf('%s#[Route(%sname: \'%s\'', $indent ? ' ' : null, $path ?? null, $routeName);

if (!empty($methods)) {
$attribute .= ', methods: [';
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/make-crud/expected/WithCustomRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#[Route('/sweet/food')]
final class SweetFoodController extends AbstractController
{
#[Route('/', name: 'app_sweet_food_index', methods: ['GET'])]
#[Route(name: 'app_sweet_food_index', methods: ['GET'])]
public function index(SweetFoodRepository $sweetFoodRepository): Response
{
return $this->render('sweet_food/index.html.twig', [
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/make-crud/tests/it_generates_basic_crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class GeneratedCrudControllerTest extends WebTestCase
public function testIndexAction()
{
$client = self::createClient();
$crawler = $client->request('GET', '/sweet/food/');
$crawler = $client->request('GET', '/sweet/food');
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertStringContainsString('<!DOCTYPE html>', $client->getResponse()->getContent());
$this->assertStringContainsString('SweetFood index', $client->getResponse()->getContent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class GeneratedCrudControllerTest extends WebTestCase
public function testIndexAction()
{
$client = self::createClient();
$crawler = $client->request('GET', '/sweet/food/admin/');
$crawler = $client->request('GET', '/sweet/food/admin');
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertStringContainsString('<!DOCTYPE html>', $client->getResponse()->getContent());
$this->assertStringContainsString('SweetFood index', $client->getResponse()->getContent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class GeneratedCrudControllerTest extends WebTestCase
public function testIndexAction()
{
$client = self::createClient();
$crawler = $client->request('GET', '/sweet/food/');
$crawler = $client->request('GET', '/sweet/food');
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertStringContainsString('<!DOCTYPE html>', $client->getResponse()->getContent());
$this->assertStringContainsString('SweetFood index', $client->getResponse()->getContent());
Expand Down

0 comments on commit 09089bc

Please sign in to comment.