Symfony转换为字符串URL

时间:2015-09-24 13:42:46

标签: php symfony url

我使用此方法打印转换为字符串的整数变量:

public function __toString(){
    return strval($this->id);
}

我在我的twig文件中打印它并且它工作得很好但是当我的变量放在这样的URL路径中时我遇到了问题:

<a href="{{ path('profilo_secondlevel', {'uid': user.idUserReferenced}) }}">{{ user.idUserReferenced }}</a>

我的网址是这样看的:

 <a href="/app_dev.php/profilo/secondo-livello?uid%5B__isInitialized__%5D=1">73</a>

我读到了这个问题,这称为百分比编码,用于编码URL参数值中的特殊字符。 但是我想在URL中输入我的身份证号码......

我该怎么办?

CODE OF routing.yml

    ################################################################################
 #                               /profile/secondo_livello                                    # 
 profilo_secondlevel:
host:      "{_locale}.{domain}"
locales:  { it: "/profilo/secondo-livello.{_format}", fr: "/profilo/secondo-livello.{_format}", de: "/profilo/secondo-livello.{_format}" }
defaults:  { _locale: "%locale%", domain: "%domain%", _format: "html", _controller: DtEcBundle:Profile:secondLevel }
requirements:
    _locale: "it|fr|de"
    domain:  "%domain%"
    _format:  "html|json"

 profilo_secondlevel_www.it:
path:      /profilo/secondo-livello/{uid}.{_format}
host:      "www.{domain}"
defaults:  { _locale: "%locale%", _format: "html", _controller: DtEcBundle:Profile:secondLevel }
requirements:
    _locale: "%locale%"
    domain:  "%domain%"
    _format:  "html|json"
  profilo_secondlevel_nowww.it:
path:      /profilo/secondo-livello/{uid}.{_format}
host:      "{domain}"
defaults:  { _locale: "%locale%",  _format: "html", _controller: DtEcBundle:Profile:secondLevel }
requirements:
    _locale: "%locale%"
    domain:  "%domain%"
    _format:  "html|json"

1 个答案:

答案 0 :(得分:1)

假设您已经定义了一个服务于profilo_secondlevel路由的控制器,您将拥有如下路由配置:

应用/配置/ routing.yml中

profilo_secondlevel:
  host:      "{_locale}.{domain}"
  path:      /profilo/secondo-livello/{uid}.{_format}
  defaults:  { _locale: "%locale%", domain: "%domain%", _format: "html", _controller: DtEcBundle:Profile:secondLevel }
  requirements:
    _locale: "it|fr|de"
    domain:  "%domain%"
    _format:  "html|json"

然后您可以在树枝模板上使用以下模板代码:

<a href="{{ path('profilo_secondlevel', {'uid': user.idUserReferenced}) }}">
{{ user.idUserReferenced }}
</a>

它应该会生成如下的网址:/profilo/secondo-livello/73.html