Symfony/Doctrine:反序列化在行动与模板

2024-08-15php开发问题
0

本文介绍了Symfony/Doctrine:反序列化在行动与模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

谁能告诉我为什么调用反序列化"在操作中可以正常工作,但在模板中会出现偏移错误?

Can anyone tell me why calling "unserialize" works fine in an action but gives an offset error in a template?

基本上可以将数据库文本结果反序列化为操作中的变量并将其传递给模板,在这种情况下它可以正常显示:

It's basically possible to unserialize a database text result into a variable in an action and pass it to template, in which case it displays fine:

$this->clean = unserialize($this->raw);
<?php echo $clean ?>

但如果直接在模板中调用则不会:

But not if called directly in a template:

<?php echo unserialize($raw) ?>

想知道为什么会这样以及是否有一些解决方法.

Would be interested in knowing why this is so and whether there's some workaround.

谢谢.

推荐答案

Symfony 将所有模板变量放入一个 sfOutputEscaperArrayDecorator 类.所以当你编写 unserialize($var) 时,实际上是在尝试对 sfOutputEscaperArrayDecorator 类进行反序列化.

Symfony puts all template variables into a sfOutputEscaperArrayDecorator class. So when you write unserialize($var), you are actually trying to unserialize the sfOutputEscaperArrayDecorator class.

我建议在 settings.yml 中关闭输出转义:

I recommend turning off output escaping in settings.yml:

escaping_strategy:     false

这是 Symfony 中一个愚蠢的、破坏性能的、不必要的功能,需要被扼杀.

It is a stupid, performance-slaughtering, unnecessary feature of Symfony that needs murdered.

更新:

如果您关闭 escaping_strategy,您将需要使用 htmlSpecialCharacters() 手动转义用户的输入(以防止 XSS).

If you turn off escaping_strategy, you will need to manually escape input from the users (to prevent XSS) with htmlSpecialCharacters().

Symfony 类会为您做到这一点,但这意味着它还会转义每一个数字和字符——您已经知道其中 99% 是安全的(ID、日期、您自己的内容).当我关闭自动转义时,我的服务器负载明显下降.

The Symfony class does that for you, but that means it also escapes every single number and character -- 99% of which you already know will be safe (IDs, dates, your own content). When I turned off the automatic escaping, my server load fell significantly.

请记住,如果您将 sfOutputEscaperArrayDecorator 传递给部分,Symfony 双重应用这种自动转义,这意味着 > 将变为 <代码>&amp;gt;

Keep in mind that Symfony double-applies this automatic escaping if you pass a sfOutputEscaperArrayDecorator to a partial, meaning > will become &amp;gt;

这篇关于Symfony/Doctrine:反序列化在行动与模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17