Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction PHP Method

urlRedirectAction() public method

By default, the response status code is 301. If the path is empty, the status code will be 410. If the permanent flag is set, the status code will be 302.
public urlRedirectAction ( string $path, boolean $permanent = false, boolean $scheme = null, integer $httpPort = 80, integer $httpsPort = 443 ) : Response
$path string The path to redirect to
$permanent boolean Whether the redirect is permanent or not
$scheme boolean The URL scheme (null to keep the current one)
$httpPort integer The HTTP port
$httpsPort integer The HTTPS port
return Symfony\Component\HttpFoundation\Response A Response instance
    public function urlRedirectAction($path, $permanent = false, $scheme = null, $httpPort = 80, $httpsPort = 443)
    {
        if (!$path) {
            return new Response(null, 410);
        }

        $request = $this->container->get('request');
        if (null === $scheme) {
            $scheme = $request->getScheme();
        }
        $qs = $request->getQueryString();
        if ($qs) {
            $qs = '?'.$qs;
        }

        $port = '';
        if ('http' === $scheme && 80 != $httpPort) {
            $port = ':'.$httpPort;
        } elseif ('https' === $scheme && 443 != $httpPort) {
            $port = ':'.$httpsPort;
        }

        $url = $scheme.'://'.$request->getHost().$port.$request->getBaseUrl().$path.$qs;

        return new RedirectResponse($url, $permanent ? 301 : 302);
    }

Usage Example

 public function testFullURL()
 {
     $controller = new RedirectController();
     $returnResponse = $controller->urlRedirectAction('http://foo.bar/');
     $this->assertRedirectUrl($returnResponse, 'http://foo.bar/');
     $this->assertEquals(302, $returnResponse->getStatusCode());
 }
All Usage Examples Of Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction