@@ -739,3 +739,51 @@ Then, configure this service ID as the ``success_handler``:
739
739
],
740
740
],
741
741
]);
742
+
743
+ Customize generated Login Link
744
+ ------------------------------
745
+
746
+ .. versionadded :: 5.3
747
+
748
+ The possibility to customize the login link was introduced in Symfony 5.3.
749
+
750
+ In some use cases it may be useful to customize the Login Link. In addition
751
+ to the ``UserInterface ``, it is therefore possible to pass a ``Request `` to the ``createLoginLink ``
752
+ method. In this example, our route is localized with the user locale which may
753
+ be different from the current locale::
754
+
755
+ // src/Controller/SecurityController.php
756
+ namespace App\Controller;
757
+
758
+ // ...
759
+ use Symfony\Component\HttpFoundation\Request;
760
+ use Symfony\Component\Security\Http\LoginLink\LoginLinkHandlerInterface;
761
+
762
+ class SecurityController extends AbstractController
763
+ {
764
+ /**
765
+ * @Route("/login", name="login")
766
+ */
767
+ public function requestLoginLink(LoginLinkHandlerInterface $loginLinkHandler, Request $request)
768
+ {
769
+ // check if login form is submitted
770
+ if ($request->isMethod('POST')) {
771
+ // ... load the user in some way
772
+
773
+ // clone and customize Request
774
+ $userRequest = clone $request;
775
+ $userRequest->setLocale($user->getLocale() ?? $request->getDefaultLocale());
776
+
777
+ // create a login link for $user this returns an instance
778
+ // of LoginLinkDetails
779
+ $loginLinkDetails = $loginLinkHandler->createLoginLink($user, $userRequest);
780
+ $loginLink = $loginLinkDetails->getUrl();
781
+
782
+ // ...
783
+ }
784
+
785
+ return $this->render('security/login.html.twig');
786
+ }
787
+
788
+ // ...
789
+ }
0 commit comments