在Symfony2中,如何在Controller中将链接重定向到带有锚标记/哈希的URL

Ell*_*oad 18 routing symfony

我在控制器中,我想重定向回URL,比如/ home/news/example#comment1423

如何将哈希值添加到返回参数中?

return $this->redirect(
    $this->generateUrl("news_view", array("permalink" => "example"))
);
Run Code Online (Sandbox Code Playgroud)

gil*_*den 28

最简单的解决方案可能是连接:

$url = $this->generateUrl("news_view", array("permalink" => "example"));
return $this->redirect(
    sprintf('%s#%s', $url, 'comment1423')
);
Run Code Online (Sandbox Code Playgroud)

  • 为什么不使用`return $ this-> redirect($ url.'#comment1424')`? (6认同)
  • 很好,我喜欢它.最佳情况是generateUrl的第3个参数 (2认同)

Dan*_*ows 7

在 Symfony 3.2 中,你可以这样做:

// generating a URL with a fragment (/settings#password)
$this->redirectToRoute('user_settings', ['_fragment' => 'password']);
Run Code Online (Sandbox Code Playgroud)

请参阅https://symfony.com/blog/new-in-symfony-3-2-routing-improvements