Hot to generate the correct URL if hardcoded in Symfony project?

◇◆丶佛笑我妖孽 提交于 2019-12-13 09:25:22

问题


When I use the function {{ path('contact_route') }} and its equivalent in controller the generated URL is correct => http://localhost/myproject/web/app_dev.php/contact.

But if I hardcode the URL (I am forced to do so in some particular cases), I get something unwanted, (a href="/contact") => http://localhost/contact


回答1:


What you are "probably" asking is how to prepend the correct base URL to your manual built URL, i.e. something like this:

a href="{{ app.request.baseUrl }}/contact"

or if you need the FQDN URL:

a href="{{ app.request.getSchemeAndHttpHost() }}/contact"



回答2:


What you expect if you set <a href="/contact">..., it's nothing more that relative url which point to the referring page plus '/contact'.



来源:https://stackoverflow.com/questions/32258236/hot-to-generate-the-correct-url-if-hardcoded-in-symfony-project

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