8000 minor #15163 [Security] Customize generated Login Link (roromix) · symfony/symfony-docs@5137e22 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5137e22

Browse files
committed
minor #15163 [Security] Customize generated Login Link (roromix)
This PR was squashed before being merged into the 5.3-dev branch. Discussion ---------- [Security] Customize generated Login Link Fix #15147 and add documentation for symfony/symfony#40641 Commits ------- 830b1f3 [Security] Customize generated Login Link
2 parents e43d541 + 830b1f3 commit 5137e22

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

security/login_link.rst

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,3 +739,51 @@ Then, configure this service ID as the ``success_handler``:
739739
],
740740
],
741741
]);
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

Comments
 (0)
0