TargetPathTrait.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Security\Http\Util;
  11. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  12. /**
  13. * Trait to get (and set) the URL the user last visited before being forced to authenticate.
  14. */
  15. trait TargetPathTrait
  16. {
  17. /**
  18. * Sets the target path the user should be redirected to after authentication.
  19. *
  20. * Usually, you do not need to set this directly.
  21. */
  22. private function saveTargetPath(SessionInterface $session, string $firewallName, string $uri)
  23. {
  24. $session->set('_security.'.$firewallName.'.target_path', $uri);
  25. }
  26. /**
  27. * Returns the URL (if any) the user visited that forced them to login.
  28. */
  29. private function getTargetPath(SessionInterface $session, string $firewallName): ?string
  30. {
  31. return $session->get('_security.'.$firewallName.'.target_path');
  32. }
  33. /**
  34. * Removes the target path from the session.
  35. */
  36. private function removeTargetPath(SessionInterface $session, string $firewallName)
  37. {
  38. $session->remove('_security.'.$firewallName.'.target_path');
  39. }
  40. }