From 9fcc5a451b601c2561ee3da3ede47b8832441bc2 Mon Sep 17 00:00:00 2001 From: ghost Date: Tue, 3 Oct 2023 22:52:26 +0300 Subject: [PATCH] make redirect to user locale selected #19 --- src/Controller/DashboardController.php | 14 ++++++++++++-- src/Controller/UserController.php | 11 ++++++++++- src/Service/UserService.php | 5 ++++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/Controller/DashboardController.php b/src/Controller/DashboardController.php index ac8b665..817fd3b 100644 --- a/src/Controller/DashboardController.php +++ b/src/Controller/DashboardController.php @@ -7,15 +7,25 @@ use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; +use App\Service\UserService; + class DashboardController extends AbstractController { #[Route('/')] - public function root(): Response + public function root( + Request $request, + UserService $userService + ): Response { + // Init user + $user = $userService->init( + $request->getClientIp() + ); + return $this->redirectToRoute( 'dashboard_index', [ - '_locale' => $this->getParameter('app.locale') + '_locale' => $user->getLocale() ] ); } diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index c0b81ec..5d685f7 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -25,7 +25,8 @@ class UserController extends AbstractController public function profile( Request $request, UserService $userService, - TimeService $timeService): Response + TimeService $timeService + ): Response { // Init user $user = $userService->init( @@ -53,6 +54,14 @@ class UserController extends AbstractController // Save changes to DB $userService->save($user); + + // Redirect user to new locale + return $this->redirectToRoute( + 'user_profile', + [ + '_locale' => $user->getLocale() + ] + ); } // Generate identicon diff --git a/src/Service/UserService.php b/src/Service/UserService.php index fc396c4..856fa9e 100644 --- a/src/Service/UserService.php +++ b/src/Service/UserService.php @@ -14,7 +14,10 @@ class UserService private UserRepository $userRepository; private ParameterBagInterface $parameterBagInterface; - public function __construct(EntityManagerInterface $entityManager, ParameterBagInterface $parameterBagInterface) + public function __construct( + EntityManagerInterface $entityManager, + ParameterBagInterface $parameterBagInterface + ) { $this->entityManager = $entityManager; $this->userRepository = $entityManager->getRepository(User::class);