Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug #5853 Priority of the getDashboardDto routeName fixed. (inalbilal)
This PR was merged into the 4.x branch. Discussion ---------- Priority of the getDashboardDto routeName fixed. In our application, when we log in using the "/customer/admin" URL, the login process is successful. However, there is an unexpected behavior with the "AdminUrlGenerator" object while generating URLs. Instead of producing the "customer_admin" value, it produces the "publisher_admin" value. This issue is occurring because the "publisher_admin" route definition takes precedence over the "customer_admin" route definition. Looking at our code, we have a controller named "DashboardController" with a method named "index": ```php #[Route('/publisher/admin', name: 'publisher_admin')] #[Route('/customer/admin', name: 'customer_admin')] public function index(): Response { $adminUrlGenerator = $this->container->get(AdminUrlGenerator::class); return $this->redirect($adminUrlGenerator->setRoute('my_route')->generateUrl()); } ``` The root cause of the issue is that the "getDashboardDto" method is unable to select the correct route definition. Since the "publisher_admin" route definition comes before the "customer_admin" route definition, the method prioritizes it, resulting in users being redirected to the "/publisher/admin" URL and encountering "Access denied" errors. To resolve this problem; ```php // AdminContextFactory.php private function getDashboardDto(Request $request, DashboardControllerInterface $dashboardControllerInstance): DashboardDto { $dashboardRoutesCachePath = $this->cacheDir.'/'.CacheWarmer::DASHBOARD_ROUTES_CACHE; $dashboardControllerRoutes = !file_exists($dashboardRoutesCachePath) ? [] : require $dashboardRoutesCachePath; // [ "publisher_admin" => "App\Controller\Admin\DashboardController::index" "customer_admin" => "App\Controller\Admin\DashboardController::index" ] $dashboardController = $dashboardControllerInstance::class.'::index'; $dashboardRouteName = null; $currentRouteName = $request->attributes->get('_route'); foreach ($dashboardControllerRoutes as $routeName => $controller) { if ($controller === $dashboardController && $currentRouteName === $routeName) { $dashboardRouteName = preg_replace('~\.\w{2}$~', '', $routeName); break; } } . . . $dashboardDto = $dashboardControllerInstance->configureDashboard()->getAsDto(); $dashboardDto->setRouteName($dashboardRouteName); return $dashboardDto; } ``` Commits ------- 0a9ff07 Priority of the getDashboardDto routeName fixed.
- Loading branch information
8e35082
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guys this simple change made a 500 internal server error on my project. I have several actions defined in my DashboardController and when I try to access them by their custom route /admin/customroute I got a 500 error with the following error log:
[Fri Aug 04 13:59:41.832936 2023] [fastcgi:error] [pid 6439] [client x.x.x.x:y] FastCGI: server "php8.1" stderr: PHP message: [critical] Uncaught PHP Exception RuntimeException: "The name of the route associated to "App\\Controller\\Admin\\DashboardController::index" cannot be determined. Clear the application cache to run the EasyAdmin cache warmer, which generates the needed data to find this route." at /vendor/easycorp/easyadmin-bundle/src/Factory/AdminContextFactory.php line 92
8e35082
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Atko I'm sorry about this. Please, try to create an issue with a few details so we can check what happens. Thanks!
8e35082
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same problem as Atko... Upgraded Symfony to 6.3.3 and updated the EA to 4.7.1 and now half of my system does not work and returns the mentioned exception...