vendor/symfony/framework-bundle/Controller/RedirectController.php line 114

Open in your IDE?
  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\Bundle\FrameworkBundle\Controller;
  11. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpKernel\Exception\HttpException;
  17. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  18. /**
  19.  * Redirects a request to another URL.
  20.  *
  21.  * @author Fabien Potencier <fabien@symfony.com>
  22.  *
  23.  * @final since version 3.4
  24.  */
  25. class RedirectController implements ContainerAwareInterface
  26. {
  27.     /**
  28.      * @deprecated since version 3.4, to be removed in 4.0
  29.      */
  30.     protected $container;
  31.     private $router;
  32.     private $httpPort;
  33.     private $httpsPort;
  34.     public function __construct(UrlGeneratorInterface $router null$httpPort null$httpsPort null)
  35.     {
  36.         $this->router $router;
  37.         $this->httpPort $httpPort;
  38.         $this->httpsPort $httpsPort;
  39.     }
  40.     /**
  41.      * @deprecated since version 3.4, to be removed in 4.0 alongside with the ContainerAwareInterface type.
  42.      */
  43.     public function setContainer(ContainerInterface $container null)
  44.     {
  45.         @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 3.4 and will be removed in 4.0. Inject an UrlGeneratorInterface using the constructor instead.'__METHOD__), E_USER_DEPRECATED);
  46.         $this->container $container;
  47.         $this->router $container->get('router');
  48.     }
  49.     /**
  50.      * Redirects to another route with the given name.
  51.      *
  52.      * The response status code is 302 if the permanent parameter is false (default),
  53.      * and 301 if the redirection is permanent.
  54.      *
  55.      * In case the route name is empty, the status code will be 404 when permanent is false
  56.      * and 410 otherwise.
  57.      *
  58.      * @param Request    $request          The request instance
  59.      * @param string     $route            The route name to redirect to
  60.      * @param bool       $permanent        Whether the redirection is permanent
  61.      * @param bool|array $ignoreAttributes Whether to ignore attributes or an array of attributes to ignore
  62.      *
  63.      * @return Response A Response instance
  64.      *
  65.      * @throws HttpException In case the route name is empty
  66.      */
  67.     public function redirectAction(Request $request$route$permanent false$ignoreAttributes false)
  68.     {
  69.         if ('' == $route) {
  70.             throw new HttpException($permanent 410 404);
  71.         }
  72.         $attributes = [];
  73.         if (false === $ignoreAttributes || \is_array($ignoreAttributes)) {
  74.             $attributes $request->attributes->get('_route_params');
  75.             unset($attributes['route'], $attributes['permanent'], $attributes['ignoreAttributes']);
  76.             if ($ignoreAttributes) {
  77.                 $attributes array_diff_key($attributesarray_flip($ignoreAttributes));
  78.             }
  79.         }
  80.         return new RedirectResponse($this->router->generate($route$attributesUrlGeneratorInterface::ABSOLUTE_URL), $permanent 301 302);
  81.     }
  82.     /**
  83.      * Redirects to a URL.
  84.      *
  85.      * The response status code is 302 if the permanent parameter is false (default),
  86.      * and 301 if the redirection is permanent.
  87.      *
  88.      * In case the path is empty, the status code will be 404 when permanent is false
  89.      * and 410 otherwise.
  90.      *
  91.      * @param Request     $request   The request instance
  92.      * @param string      $path      The absolute path or URL to redirect to
  93.      * @param bool        $permanent Whether the redirect is permanent or not
  94.      * @param string|null $scheme    The URL scheme (null to keep the current one)
  95.      * @param int|null    $httpPort  The HTTP port (null to keep the current one for the same scheme or the configured port in the container)
  96.      * @param int|null    $httpsPort The HTTPS port (null to keep the current one for the same scheme or the configured port in the container)
  97.      *
  98.      * @return Response A Response instance
  99.      *
  100.      * @throws HttpException In case the path is empty
  101.      */
  102.     public function urlRedirectAction(Request $request$path$permanent false$scheme null$httpPort null$httpsPort null)
  103.     {
  104.         if ('' == $path) {
  105.             throw new HttpException($permanent 410 404);
  106.         }
  107.         $statusCode $permanent 301 302;
  108.         // redirect if the path is a full URL
  109.         if (parse_url($pathPHP_URL_SCHEME)) {
  110.             return new RedirectResponse($path$statusCode);
  111.         }
  112.         if (null === $scheme) {
  113.             $scheme $request->getScheme();
  114.         }
  115.         $qs $request->getQueryString();
  116.         if ($qs) {
  117.             if (false === strpos($path'?')) {
  118.                 $qs '?'.$qs;
  119.             } else {
  120.                 $qs '&'.$qs;
  121.             }
  122.         }
  123.         $port '';
  124.         if ('http' === $scheme) {
  125.             if (null === $httpPort) {
  126.                 if ('http' === $request->getScheme()) {
  127.                     $httpPort $request->getPort();
  128.                 } elseif ($this->container && $this->container->hasParameter('request_listener.http_port')) {
  129.                     @trigger_error(sprintf('Passing the http port as a container parameter is deprecated since Symfony 3.4 and won\'t be possible in 4.0. Pass it to the constructor of the "%s" class instead.'__CLASS__), E_USER_DEPRECATED);
  130.                     $httpPort $this->container->getParameter('request_listener.http_port');
  131.                 } else {
  132.                     $httpPort $this->httpPort;
  133.                 }
  134.             }
  135.             if (null !== $httpPort && 80 != $httpPort) {
  136.                 $port ":$httpPort";
  137.             }
  138.         } elseif ('https' === $scheme) {
  139.             if (null === $httpsPort) {
  140.                 if ('https' === $request->getScheme()) {
  141.                     $httpsPort $request->getPort();
  142.                 } elseif ($this->container && $this->container->hasParameter('request_listener.https_port')) {
  143.                     @trigger_error(sprintf('Passing the https port as a container parameter is deprecated since Symfony 3.4 and won\'t be possible in 4.0. Pass it to the constructor of the "%s" class instead.'__CLASS__), E_USER_DEPRECATED);
  144.                     $httpsPort $this->container->getParameter('request_listener.https_port');
  145.                 } else {
  146.                     $httpsPort $this->httpsPort;
  147.                 }
  148.             }
  149.             if (null !== $httpsPort && 443 != $httpsPort) {
  150.                 $port ":$httpsPort";
  151.             }
  152.         }
  153.         $url $scheme.'://'.$request->getHost().$port.$request->getBaseUrl().$path.$qs;
  154.         return new RedirectResponse($url$statusCode);
  155.     }
  156. }