Symfony 1.4:是否可以防止重定向 URL 转义?
如果我正常执行重定向:
$this->redirect('@mypage?apple=1&banana=2&orange=3');
... Symfony 生成正确的 URL:
/something/something?apple=1&banana=2&orange=3
但是,由于某些奇怪的原因,以下内容被转义:
$string = 'apple=1&banana=2&orange=3';
$this->redirect('@mypage?'.$string);
... 并生成以下 URL:
/something/something?apple=1&banana=2&orange=3
有没有办法避免这种转义并有URL 中的 & 符号是否正确显示?我已经尝试了所有我能想到的方法,但这让我发疯。当我从数据库中以字符串形式提取保存的查询并且只想将其锁定到 URL 时,我需要这个。我知道我可以从字符串生成一个数组,然后从该数组生成一个全新的 URL,但由于这种愚蠢的转义,它看起来像是很多开销。
谢谢。
If I do a redirect in action as normal:
$this->redirect('@mypage?apple=1&banana=2&orange=3');
... Symfony produces the correct URL:
/something/something?apple=1&banana=2&orange=3
However, the following gets escaped for some bizarre reason:
$string = 'apple=1&banana=2&orange=3';
$this->redirect('@mypage?'.$string);
... and the following URL is produced:
/something/something?apple=1&banana=2&orange=3
Is there a way to avoid this escaping and have the ampersands appear correctly in the URL? I've tried everything I can think of and it's driving me mad. I need this for a situation where I'm pulling a saved query as a string from the database and would just like to latch it onto the URL. I'm aware that I could generate an array from the string and then generate a brand new URL from the array, but it just seems like a lot of overhead because of this silly escaping.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您说“正确显示”时,您可能会忽略
&
是 URL 中变量分隔符&
的“正确”表示形式 - 好吧-formed X(HT)ML,&
必须编码为&
也就是说,脚本中的行为是不寻常的,因为您期望相同两个示例中的行为。我想这些不是路由规则的特定于路由的参数,尽管我希望 symfony 默认会像这样削减你的变量:
这不是一些奇怪的 PHP 设置干扰你的字符串,是吗?
编辑:我已经用 symfony 1.3.3 对其进行了测试,它按照我的建议削减了变量。我倾向于认为这是 PHP/Apache 设置造成的。检查 php.ini 文件中的 arg_separator.output 设置。
When you say "appear correctly", you might be missing the fact that
&
is the 'correct' representation of the variable separator&
in URLs - for well-formed X(HT)ML,&
must be encoded as&
That said, the behaviour in your script is unusual in that you'd expect the same behaviour in both examples. I guess these aren't routing-specific parameters to your routing rule, although I would expect symfony as default to slashify your variables like this:
It's not some odd PHP setting that's interfering with your strings, is it?
EDIT: I've tested it with symfony 1.3.3, and it's slashifying the variables as I suggested. I'm inclined to think it's a PHP/Apache setting that's causing it. Check your php.ini file for the arg_separator.output setting.
我相信这是你最好的选择。
这对我来说效果很好( & 不会转义为 &)
I believe this is your best bet.
This works fine for me (the &'s are not escaped to &)