How to use anchors in Symfony routing?

做~自己de王妃 提交于 2019-12-23 12:08:11

问题


I have defined a route as followed in my routing.yml file :

route_name:
    path: "/dashboard#messages/{id}"

However when I ask Symfony to generate that route, I get :

/dashboard%23messages/12345

How can I skip the encoding part of the route generation? Or how can I escape the # char in the path definition?

PS : Working with a (big) legacy system, I cannot change the urls.


回答1:


Available from Symfony 3.2.

Support for anchors has been announced for the routing component using the fragment variable :

$this->get('router')->generate('user_settings', ['_fragment' => 'password']);

Will generate an url : /user/settings#password

For more information view the announcement.




回答2:


You cannot easily - route parts are encoded unconditionally:

$url = strtr(rawurlencode($url), $this->decodedChars);

see at https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Routing/Generator/UrlGenerator.php#L192

Technically you might have extended the class UrlGenerator class and swap them using router.options.generator_class parameter. Then you could override the doGenerate method and replace %23 -> #.




回答3:


In twig

<a href="{{ path('user_settings', { '_fragment': 'password' }) }}">


来源:https://stackoverflow.com/questions/36910829/how-to-use-anchors-in-symfony-routing

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