2016-02-11 32 views
0

我有重定向到其他網站的問題。當我重定向時,我看到消息:「重定向到...」。爲什麼?真的,我無法重定向到沒有問題的網站?Symfony2 RedirectResponse消息

我看到: Is it possible to change the default redirect message in Symfony?

我的代碼:

/** 
    * @Route("/project/{url}/{link}/", name="page_show") 
    * @Template("base.html.twig") 
    */ 
    public function pageAction($link, $url) { 

    if ($link == '...') { 
     $redrect = new RedirectResponse('http://...'); 
     return $redrect; 
    } 

也許我是個白癡,看不到解決方案...

+0

你貼什麼看起來像它應該工作得很好。你確定你的重定向網址是正確的嗎?會發生什麼,你複製/粘貼到瀏覽器? – Cerad

+0

網址無誤。我看到消息,然後我被重定向 – viko

+0

所有我能想到的是,你有某種聽衆的攔截這也許您的主機有某種限制的事情。我會用簡單的http重定向來製作一個簡單的redirect.php文件,以排除任何主機謎題。 http://php.net/manual/en/function.header.php – Cerad

回答

1

我不知道,你可以結合@Template("base.html.twig")與重定向響應。嘗試刪除@template註釋和做你的行動結束渲染base.html.twig的:

/** 
* @Route("/project/{url}/{link}/", name="page_show") 
*/ 
public function pageAction($link, $url) { 

    if ($link == '...') { 
     $redrect = new RedirectResponse('http://...'); 
     return $redrect; 
    } 

    // Maybe add the proper path "BundleName:DirectoryName:base.html.twig" 
    return $this->render('base.html.twig'); 
} 
+0

我認爲這是解決方案。當然不起作用:( – viko

1

重定向到另一個網址:

public function pageAction(Request $request) 
{ 
    return $this->redirect('https://google.com'); 
} 
1

如果這是在開發環境發生的事情,那麼你已經設置intercept_redirects配置選項true。將其設置爲false中解釋說:http://symfony.com/doc/current/reference/configuration/web_profiler.html

如果這是在生產環境發生,其原因就在於RedirectResponse有一些硬編碼的HTML內容做重定向。請參閱以下幾行代碼:https://github.com/symfony/symfony/blob/2.8/src/Symfony/Component/HttpFoundation/RedirectResponse.php#L86-L96

在1秒鐘內,您會看到重定向到...消息。改變這個信息是可能的,但它需要你付出很大的努力。一切都在這裏解釋:Is it possible to change the default redirect message in Symfony?


更新:在this discussion in the Symfony repository有更多這方面的信息。重定向應該是即時的,因爲它應該使用響應頭中提供的信息。如果發生錯誤,則使用硬編碼的HTML內容。所以你可能需要調試爲什麼響應沒有得到正確的重定向頭。

0

除非你真的熱衷使用的Symfony」 RedirectResponse類重定向,您可以在老好PHP總是繼電器:

function redirectUrl($url, $replace=true, $status=302){ 
    header("Location : ".$url, $replace, $status); 
    exit(); 
} 

// usage example 
redirectUrl("http://www.google.com"); 

這不僅用得好好的,但速度極快與Symfony的的比較內部功能,因爲只有一個電話+死亡/退出。

瞭解更多關於PHP的header功能。

然而,由於這種方法是完全從框架分離時,Symfony的分析器將無法攔截您重定向使開發人員工具欄中不會注意到/顯示此重定向。這取決於你想要的。

相關問題