我有几个URL,如下所示:
{{domainID}}/action/{{userId}}/anotherAction
后一个URL指向:
http://localhost/viewA/{{domainID}}/action/{{userId}}/anotherAction
然而,如果我试图通过iframe从viewB
加载viewA
,则viewA
内的链接而不是指向:
http://localhost/viewA/{{domainID}}/action/{{userId}}/anotherAction
它将指向:
http://localhost/viewB/{{domainID}}/action/{{userId}}/anotherAction
并且如果用户跟随后者,则用户将最终进入404页面。
我的问题是:
有没有办法在trick中获得以这种方式构建的url的绝对路径?
编辑
路线定义为:
@Route("/domain/details/{domainId}", name="domain_detailed_view")
我试图通过这种方式获得绝对路径:
{{ url({{domainID}}/action/{{userId}}/anotherAction) }}
但我得到了这个错误:
哈希键必须是带引号的字符串、数字、名称或表达式括号内
url
或path
函数采用路由名称,而不是路径。如果路由需要参数,则可以为其提供一个关联数组作为可选的第二个参数。
例如:
{{ url('domain_detailed_view', { 'domainId': domainId, 'userId': userId }) }}
http://symfony.com/doc/master/reference/twig_reference.html
我知道它很旧,有答案,但有symfony 3&你可以做的树枝:
{{ app.request.getSchemeAndHttpHost() }}
/* will match even port :) i.e.
* http://localhost:8000
* http://localhost
* http://example.com
*/
这是非常有用的:D
有两种方法可以做同样的事情通常,您可以在absolute_url函数中使用url()
或path,如soabsoulute_url(path(…))
// example 1:
{{ url('domain_detailed_view', { 'domainId': domainId, 'userId': userId }) }}
// example 2:
{{ absolute_url(path('domain_detailed_view', { 'domainId': domainId, 'userId': userId })) }}">
// note - those two do the same thing
一般来说,自Symfony 2.7以来,您可以使用absolute_url()和assert以及相对路径(相对于web/root文件夹)。这就是如何使用它们来设置捆绑包和主web文件夹中图像的绝对路径:
[app/src/UserBundle/Resources/public/img/image.jpg]
<img src="{{ absolute_url(asset('bundle/user/img/image.jpg')) }}" /> // new way since 2.7 up
<img src="{{ asset('bundle/user/img/image.jpg', absolute: true ) }}" /> // old way below 2.7 removed in symfony 3.0
[web/css/img/some.jpg]
<img src="{{ absolute_url('css/img/some.jpg') }}" />
这是symfony建议在呈现电子邮件视图时使用的方法。http://symfony.com/doc/current/email.html