Skip to content

Commit 1964efe

Browse files
committed
bug EasyCorp#7477 Fix route generation for entities called Controller (javiereguiluz)
This PR was squashed before being merged into the 4.x branch. Discussion ---------- Fix route generation for entities called Controller Fixes EasyCorp#7167. Commits ------- 9b0d71b Fix route generation for entities called Controller
2 parents d193f60 + 9b0d71b commit 1964efe

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

src/Router/AdminRouteGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ private function getPhpAttributeInstance(string $classFqcn, string $attributeFqc
882882
*/
883883
private function transformCrudControllerNameToKebabCase(string $crudControllerFqcn): string
884884
{
885-
$cleanShortName = str_replace(['CrudController', 'Controller'], '', (new \ReflectionClass($crudControllerFqcn))->getShortName());
885+
$cleanShortName = preg_replace('/(?:Crud)?Controller$/', '', (new \ReflectionClass($crudControllerFqcn))->getShortName());
886886
$snakeCaseName = strtolower(preg_replace('/(?<!^)[A-Z]/', '-$0', $cleanShortName));
887887

888888
return $snakeCaseName;
@@ -895,7 +895,7 @@ private function transformCrudControllerNameToKebabCase(string $crudControllerFq
895895
*/
896896
private function transformCrudControllerNameToSnakeCase(string $crudControllerFqcn): string
897897
{
898-
$shortName = str_replace(['CrudController', 'Controller'], '', (new \ReflectionClass($crudControllerFqcn))->getShortName());
898+
$shortName = preg_replace('/(?:Crud)?Controller$/', '', (new \ReflectionClass($crudControllerFqcn))->getShortName());
899899

900900
return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $shortName));
901901
}

tests/Functional/AdminRoute/AdminRouteTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,26 @@ public function testLegacyLinkToCrudMenuItemStillWorks(): void
612612
$this->assertInstanceOf(CrudMenuItem::class, $menuItem);
613613
}
614614

615+
public function testCrudControllerNamedController(): void
616+
{
617+
$client = static::createClient();
618+
$router = $client->getContainer()->get('router');
619+
620+
// test that ControllerCrudController generates routes with "controller" as the path segment
621+
// (this was previously broken because str_replace removed "Controller" from the middle of the name)
622+
$indexRoute = $router->getRouteCollection()->get('admin_controller_index');
623+
$this->assertNotNull($indexRoute, 'Route "admin_controller_index" should exist');
624+
$this->assertSame('/admin/controller', $indexRoute->getPath());
625+
626+
$detailRoute = $router->getRouteCollection()->get('admin_controller_detail');
627+
$this->assertNotNull($detailRoute, 'Route "admin_controller_detail" should exist');
628+
$this->assertSame('/admin/controller/{entityId}', $detailRoute->getPath());
629+
630+
// test that routes also exist for the second dashboard
631+
$this->assertNotNull($router->getRouteCollection()->get('second_admin_controller_index'));
632+
$this->assertNotNull($router->getRouteCollection()->get('second_admin_controller_detail'));
633+
}
634+
615635
public function testAdminDashboardAdvancedRouteOptions(): void
616636
{
617637
$client = static::createClient();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\AdminRouteApp\Controller;
4+
5+
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
6+
use EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\AdminRouteApp\Entity\Product;
7+
8+
/**
9+
* Test CRUD controller whose class name triggers the suffix stripping bug
10+
* reported in https://github.com/EasyCorp/EasyAdminBundle/issues/7167.
11+
* The entity name "Controller" causes the auto-generated route to be empty
12+
* when using str_replace instead of suffix-only stripping.
13+
*/
14+
class ControllerCrudController extends AbstractCrudController
15+
{
16+
public static function getEntityFqcn(): string
17+
{
18+
return Product::class;
19+
}
20+
}

0 commit comments

Comments
 (0)