问题描述
当我使用函数 {{ path('contact_route') }} 及其在控制器中的等效项时,生成的 URL 是正确的 => http://localhost/myproject/web/app_dev.php/contact.
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.
但是如果我对 URL 进行硬编码(在某些特定情况下我被迫这样做),我会得到一些不需要的东西,(a href="/contact") => http://localhost/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
推荐答案
您可能"问的是如何将正确的基本 URL 添加到您手动构建的 URL 中,例如:
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"
或者如果您需要 FQDN URL:
or if you need the FQDN URL:
a href="{{ app.request.getSchemeAndHttpHost() }}/contact"
这篇关于如果在 Symfony 项目中进行硬编码,是否可以生成正确的 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!