src/Action/RedirectAction.php line 26

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Action;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. /**
  7. * Class RedirectAction
  8. *
  9. * @author André Varelmann <andre.varelmann@bestit-online.de>
  10. * @package App\Action
  11. */
  12. class RedirectAction
  13. {
  14. /**
  15. * Redirects to a given target route
  16. *
  17. * @param string $path
  18. * @param bool $permanent
  19. *
  20. * @return RedirectResponse
  21. */
  22. public function __invoke(
  23. string $path,
  24. bool $permanent = false,
  25. ?Request $request = null,
  26. ): RedirectResponse
  27. {
  28. if (isset($request)) {
  29. $allQueryParams = $request->query->all();
  30. if (!empty($allQueryParams) && !str_contains($path, '?')) {
  31. $path .= '?' . http_build_query($allQueryParams);
  32. }
  33. }
  34. return new RedirectResponse(
  35. $path,
  36. $permanent ? 301 : 302
  37. );
  38. }
  39. }