In Symfony2 how do I redirect within a Controller to a URL with an anchor tag/hash

前提是你 提交于 2019-12-03 07:01:24

问题


I'm in a Controller and I want to redirect back to a URL, say /home/news/example#comment1423

How can I add the hash in to the return params?

return $this->redirect(
    $this->generateUrl("news_view", array("permalink" => "example"))
);

回答1:


The simplest solution would probably be concatenation:

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



回答2:


In Symfony 3.2 you can do this:

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

See https://symfony.com/blog/new-in-symfony-3-2-routing-improvements



来源:https://stackoverflow.com/questions/7724082/in-symfony2-how-do-i-redirect-within-a-controller-to-a-url-with-an-anchor-tag-ha

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!