问题
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