如何让Symfony在所有响应中指定我的字符集?

时间:2022-06-28 20:07:18

All responses from my Symfony 2 application all have this header line:

我的Symfony 2应用程序的所有响应都有这样的标题行:

Content-Type: text/html

So my browser assumes the output charset is ISO-8859-1. I would like the header to say:

我的浏览器假设输出字符集是ISO-8859-1。我希望标题说:

Content-Type: text/html; charset=utf8

instead. I know I can do this manually for every response like this:

代替。我知道我可以为这样的每一个回复手动操作:

$response->setCharset('UTF-8');

But that would be incredibly redundant since my application has lots of controls and will only ever output UTF-8. I have searched in vain for a Symfony configuration setting that controls the default charset. I found framework.charset but that seems to be unrelated, and is UTF-8 by default anyway.

但是这将是非常多余的,因为我的应用程序有很多控件,并且只输出UTF-8。我徒劳地搜索了Symfony配置设置,该设置控制默认字符集。我发现框架。charset但是这似乎是不相关的,并且默认是UTF-8。

How do I get Symfony to specify my charset in all responses?

如何让Symfony在所有响应中指定我的字符集?


My thoughts

I know this isn't too much of a problem (most of the time) since I can specify the charset explicitly in both HTML and XML. The issue is when I'm service plain text or JSON, or if I'm dumping variables using var_dump. In both cases, all non-ASCII symbols are scrambled as my browser ventures an erroneous guess.

我知道这不是什么大问题(大多数时候),因为我可以在HTML和XML中显式地指定字符集。问题是当我使用纯文本或JSON服务时,或者如果我使用var_dump转储变量时。在这两种情况下,当我的浏览器冒着错误猜测的风险时,所有非ascii符号都被打乱了。

My backup solution is to subclass the response class and put $this->setCharset('UTF-8') in the constructor.

我的备份解决方案是对响应类进行子类化,并将$this->setCharset('UTF-8')放在构造函数中。

4 个解决方案

#1


1  

Using setCharset() doesn't set charset in headers.

使用setCharset()不会在标题中设置字符集。

If you want to specify utf-8 encoding in headers, you can use following code somewhere before $app->run()

如果希望在header中指定utf-8编码,可以在$app->运行()之前的某个地方使用以下代码

$app->after(
    function (Request $request, Response $response) {
        $response->headers->set(
            'Content-Type', 
            $response->headers->get('Content-Type') . '; charset=UTF-8'
        );

        return $response;
    }
);

Additionally if you want your json output to be displayed with unescaped utf8 characters, you can use something like this:

另外,如果您希望您的json输出显示为未转义的utf8字符,您可以使用如下的方法:

$app->after(
    function (Request $request, Response $response) {
        $response->headers->set('Content-Type', $response->headers->get('Content-Type') . '; charset=UTF-8');

        if ($response instanceof JsonResponse) {
            $response->setEncodingOptions(JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
        }

        return $response;
    }
);

It sets pretty print as well :)

它还印上了漂亮的图案:

#2


0  

If nothing else helps, keep using $response->setCharset('UTF-8');, but put it in a kernel response listener. This is a general pattern in Symfony: everytime you do (almost) the same thing in a lot of controllers, it should go to a kernel response listener instead to keep the controller code straight to the point.

如果没有其他的帮助,继续使用$response->setCharset('UTF-8');,但是把它放在一个内核响应监听器中。这是Symfony中的一个通用模式:每当您在许多控制器中做(几乎)相同的事情时,它应该转到内核响应侦听器,而不是将控制器代码直接保持到要点。

#3


0  

In Symfony 3.1 just override getCharset() in your AppKernel. http://symfony.com/doc/current/reference/configuration/kernel.html

在Symfony 3.1中,只需在AppKernel中覆盖getCharset()。http://symfony.com/doc/current/reference/configuration/kernel.html

// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function getCharset()
    {
        return 'ISO-8859-1';
    }
}

#4


-1  

I think you can tell the framework which charset to use in your config.yml:

我想你可以告诉我在你的config.yml中使用哪个charset。

framework:
  charset:         UTF-8

See: http://symfony.com/doc/current/reference/configuration/framework.html

参见:http://symfony.com/doc/current/reference/configuration/framework.html

#1


1  

Using setCharset() doesn't set charset in headers.

使用setCharset()不会在标题中设置字符集。

If you want to specify utf-8 encoding in headers, you can use following code somewhere before $app->run()

如果希望在header中指定utf-8编码,可以在$app->运行()之前的某个地方使用以下代码

$app->after(
    function (Request $request, Response $response) {
        $response->headers->set(
            'Content-Type', 
            $response->headers->get('Content-Type') . '; charset=UTF-8'
        );

        return $response;
    }
);

Additionally if you want your json output to be displayed with unescaped utf8 characters, you can use something like this:

另外,如果您希望您的json输出显示为未转义的utf8字符,您可以使用如下的方法:

$app->after(
    function (Request $request, Response $response) {
        $response->headers->set('Content-Type', $response->headers->get('Content-Type') . '; charset=UTF-8');

        if ($response instanceof JsonResponse) {
            $response->setEncodingOptions(JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
        }

        return $response;
    }
);

It sets pretty print as well :)

它还印上了漂亮的图案:

#2


0  

If nothing else helps, keep using $response->setCharset('UTF-8');, but put it in a kernel response listener. This is a general pattern in Symfony: everytime you do (almost) the same thing in a lot of controllers, it should go to a kernel response listener instead to keep the controller code straight to the point.

如果没有其他的帮助,继续使用$response->setCharset('UTF-8');,但是把它放在一个内核响应监听器中。这是Symfony中的一个通用模式:每当您在许多控制器中做(几乎)相同的事情时,它应该转到内核响应侦听器,而不是将控制器代码直接保持到要点。

#3


0  

In Symfony 3.1 just override getCharset() in your AppKernel. http://symfony.com/doc/current/reference/configuration/kernel.html

在Symfony 3.1中,只需在AppKernel中覆盖getCharset()。http://symfony.com/doc/current/reference/configuration/kernel.html

// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function getCharset()
    {
        return 'ISO-8859-1';
    }
}

#4


-1  

I think you can tell the framework which charset to use in your config.yml:

我想你可以告诉我在你的config.yml中使用哪个charset。

framework:
  charset:         UTF-8

See: http://symfony.com/doc/current/reference/configuration/framework.html

参见:http://symfony.com/doc/current/reference/configuration/framework.html