Skip to content

Commit e8355e5

Browse files
committed
Refactor dashboard URLs in controllers
1 parent b819e1b commit e8355e5

5 files changed

Lines changed: 16 additions & 33 deletions

File tree

src/Controller/Dashboard/DashboardAccountController.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use CodedMonkey\Dirigent\Doctrine\Repository\UserRepository;
77
use CodedMonkey\Dirigent\Form\AccountFormType;
88
use CodedMonkey\Dirigent\Form\ChangePasswordFormType;
9-
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
109
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1110
use Symfony\Component\Form\FormError;
1211
use Symfony\Component\HttpFoundation\Request;
@@ -18,13 +17,6 @@
1817

1918
class DashboardAccountController extends AbstractController
2019
{
21-
public static function getSubscribedServices(): array
22-
{
23-
return array_merge(parent::getSubscribedServices(), [
24-
AdminUrlGenerator::class => AdminUrlGenerator::class,
25-
]);
26-
}
27-
2820
public function __construct(
2921
private readonly UserRepository $userRepository,
3022
private readonly UserPasswordHasherInterface $passwordHasher,
@@ -45,9 +37,7 @@ public function account(Request $request, #[CurrentUser] User $user): Response
4537

4638
$this->addFlash('success', 'Your account was successfully updated.');
4739

48-
$url = $this->container->get(AdminUrlGenerator::class)->setRoute('dashboard_account')->generateUrl();
49-
50-
return $this->redirect($url);
40+
return $this->redirectToRoute('dashboard_account');
5141
}
5242

5343
$passwordForm->handleRequest($request);
@@ -66,9 +56,7 @@ public function account(Request $request, #[CurrentUser] User $user): Response
6656

6757
$this->addFlash('success', 'Your password was successfully updated.');
6858

69-
$url = $this->container->get(AdminUrlGenerator::class)->setRoute('dashboard_account')->generateUrl();
70-
71-
return $this->redirect($url);
59+
return $this->redirectToRoute('dashboard_account');
7260
}
7361
}
7462

src/Controller/Dashboard/DashboardPackagesController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public function addVcsRepository(Request $request): Response
238238

239239
$this->messenger->dispatch(new UpdatePackage($package->getId()));
240240

241-
return $this->redirect($this->adminUrlGenerator->setRoute('dashboard_packages')->generateUrl());
241+
return $this->redirectToRoute('dashboard_packages');
242242
}
243243

244244
return $this->render('dashboard/packages/add_vcs.html.twig', [
@@ -263,7 +263,7 @@ public function edit(Request $request, string $packageName): Response
263263

264264
$this->messenger->dispatch(new UpdatePackage($package->getId()));
265265

266-
return $this->redirect($this->adminUrlGenerator->setRoute('dashboard_packages_info', ['packageName' => $package->getName()])->generateUrl());
266+
return $this->redirectToRoute('dashboard_packages_info', ['packageName' => $package->getName()]);
267267
}
268268

269269
return $this->render('dashboard/packages/package_edit.html.twig', [
@@ -280,7 +280,7 @@ public function update(string $packageName): Response
280280

281281
$this->messenger->dispatch(new UpdatePackage($package->getId(), forceRefresh: true));
282282

283-
return $this->redirect($this->adminUrlGenerator->setRoute('dashboard_packages_info', ['packageName' => $package->getName()])->generateUrl());
283+
return $this->redirectToRoute('dashboard_packages_info', ['packageName' => $package->getName()]);
284284
}
285285

286286
#[Route('/packages/{packageName}/delete', name: 'dashboard_packages_delete', requirements: ['packageName' => '[a-z0-9_.-]+/[a-z0-9_.-]+'])]
@@ -291,6 +291,6 @@ public function delete(string $packageName): Response
291291

292292
$this->packageRepository->remove($package, true);
293293

294-
return $this->redirect($this->adminUrlGenerator->setRoute('dashboard_packages')->generateUrl());
294+
return $this->redirectToRoute('dashboard_packages');
295295
}
296296
}

src/Controller/Dashboard/DashboardResetPasswordController.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use CodedMonkey\Dirigent\Form\ResetPasswordFormType;
77
use CodedMonkey\Dirigent\Form\ResetPasswordRequestFormType;
88
use Doctrine\ORM\EntityManagerInterface;
9-
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
109
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
1110
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1211
use Symfony\Component\HttpFoundation\RedirectResponse;
@@ -27,7 +26,6 @@ public function __construct(
2726
private readonly EntityManagerInterface $entityManager,
2827
private readonly MailerInterface $mailer,
2928
private readonly ResetPasswordHelperInterface $resetPasswordHelper,
30-
private readonly AdminUrlGenerator $adminUrlGenerator,
3129
) {
3230
}
3331

@@ -64,7 +62,7 @@ public function passwordReset(Request $request, ?string $token = null): Response
6462
if ($token) {
6563
$this->storeTokenInSession($token);
6664

67-
return $this->redirect($this->adminUrlGenerator->setRoute('dashboard_reset_password')->generateUrl());
65+
return $this->redirectToRoute('dashboard_reset_password');
6866
}
6967

7068
$token = $this->getTokenFromSession();
@@ -82,7 +80,7 @@ public function passwordReset(Request $request, ?string $token = null): Response
8280
$exception->getReason()
8381
));
8482

85-
return $this->redirect($this->adminUrlGenerator->setRoute('dashboard_reset_password_request')->generateUrl());
83+
return $this->redirectToRoute('dashboard_reset_password_request');
8684
}
8785

8886
$form = $this->createForm(ResetPasswordFormType::class);
@@ -96,7 +94,7 @@ public function passwordReset(Request $request, ?string $token = null): Response
9694

9795
$this->cleanSessionAfterReset();
9896

99-
return $this->redirect($this->adminUrlGenerator->setRoute('dashboard_login')->generateUrl());
97+
return $this->redirectToRoute('dashboard_login');
10098
}
10199

102100
return $this->render('dashboard/reset_password/reset.html.twig', [
@@ -130,6 +128,6 @@ private function processSendingPasswordResetEmail(string $email): RedirectRespon
130128
}
131129
}
132130

133-
return $this->redirect($this->adminUrlGenerator->setRoute('dashboard_reset_password_sent')->generateUrl());
131+
return $this->redirectToRoute('dashboard_reset_password_sent');
134132
}
135133
}

src/Controller/Dashboard/DashboardRootController.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
1717
use EasyCorp\Bundle\EasyAdminBundle\Config\UserMenu;
1818
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
19-
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
2019
use Symfony\Component\DependencyInjection\Attribute\Autowire;
2120
use Symfony\Component\HttpFoundation\Response;
2221
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@@ -152,7 +151,6 @@ public function credits(): Response
152151

153152
private function parseDocumentationFile(string $directory, string $page): array
154153
{
155-
$adminUrlGenerator = $this->container->get(AdminUrlGenerator::class);
156154
$twig = $this->container->get('twig');
157155

158156
$template = "dashboard/docs/$directory/$page.md.twig";
@@ -178,7 +176,7 @@ private function parseDocumentationFile(string $directory, string $page): array
178176

179177
// Fix relative URLs
180178
$relativeLinkPattern = '/(\[.*?]\()([^\/)]+)(\))/';
181-
$docsUrlPattern = $adminUrlGenerator->setRoute("dashboard_{$directory}_docs", ['page' => 'pagename'])->generateUrl();
179+
$docsUrlPattern = $this->generateUrl("dashboard_{$directory}_docs", ['page' => 'pagename']);
182180
$docsUrlPattern = str_replace('pagename', '$2', $docsUrlPattern);
183181
$relativeLinkReplacementPattern = "\$1{$docsUrlPattern}\$3";
184182

src/Controller/Dashboard/DashboardSecurityController.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use CodedMonkey\Dirigent\Doctrine\Entity\User;
66
use CodedMonkey\Dirigent\Doctrine\Repository\UserRepository;
77
use CodedMonkey\Dirigent\Form\RegistrationFormType;
8-
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
98
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
109
use Symfony\Component\HttpFoundation\Request;
1110
use Symfony\Component\HttpFoundation\Response;
@@ -20,26 +19,26 @@ public function __construct(
2019
}
2120

2221
#[Route('/login', name: 'dashboard_login')]
23-
public function login(AuthenticationUtils $authenticationUtils, AdminUrlGenerator $adminUrlGenerator): Response
22+
public function login(AuthenticationUtils $authenticationUtils): Response
2423
{
2524
$userCount = $this->userRepository->count([]);
2625

2726
if (0 === $userCount) {
28-
return $this->redirect($adminUrlGenerator->setRoute('dashboard_register')->generateUrl());
27+
return $this->redirectToRoute('dashboard_register');
2928
}
3029

3130
return $this->render('@EasyAdmin/page/login.html.twig', [
3231
'action' => $this->generateUrl('dashboard_login'),
3332
'error' => $authenticationUtils->getLastAuthenticationError(),
3433
'last_username' => $authenticationUtils->getLastUsername(),
3534
'forgot_password_enabled' => true,
36-
'forgot_password_path' => $adminUrlGenerator->setRoute('dashboard_reset_password_request')->generateUrl(),
35+
'forgot_password_path' => $this->generateUrl('dashboard_reset_password_request'),
3736
'remember_me_enabled' => true,
3837
]);
3938
}
4039

4140
#[Route('/register', name: 'dashboard_register')]
42-
public function register(Request $request, AdminUrlGenerator $adminUrlGenerator): Response
41+
public function register(Request $request): Response
4342
{
4443
$registrationEnabled = $this->getParameter('dirigent.security.registration_enabled');
4544
$userCount = $this->userRepository->count([]);
@@ -61,7 +60,7 @@ public function register(Request $request, AdminUrlGenerator $adminUrlGenerator)
6160

6261
$this->userRepository->save($user, true);
6362

64-
return $this->redirect($adminUrlGenerator->setRoute('dashboard_login')->generateUrl());
63+
return $this->redirectToRoute('dashboard_login');
6564
}
6665

6766
return $this->render('dashboard/security/register.html.twig', [

0 commit comments

Comments
 (0)