Laravel 5 字符集在视图上无法正常工作。但是当我从控制器转储它时它运行良好
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30399160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5 charset not working correctly on the views. But it working well when I dump it from controller
提问by Anderson Silva
I'm facing a charset problem here. I'm developing an app that uses a sql server database. The database was not created for this app, it exists before it and works very well. I can't change anything on the database because its too large and its used by many other apps.
I've been finished the auth of my laravel 5 app, so I'll create a view and show in this view the name of logged user. The name is: ADMINISTRADOR DA ACENTUA??O. It use some special characters.
In my views:
我在这里面临字符集问题。我正在开发一个使用 sql server 数据库的应用程序。数据库不是为此应用程序创建的,它存在于它之前并且运行良好。我无法更改数据库上的任何内容,因为它太大并且被许多其他应用程序使用。
我已经完成了 laravel 5 应用程序的身份验证,因此我将创建一个视图并在该视图中显示登录用户的名称。名称是:ADMINISTRADOR DA ACENTUA??O。它使用一些特殊字符。
在我看来:
{!!Auth::user()->name!!}
it shows:
表明:
ADMINISTRADOR DA ACENTUA??O
ADMINISTRADOR DA ACENTUA??O
But in my controller, before I return the view, I did:
但是在我的控制器中,在返回视图之前,我做了:
die(\Auth::user()->name);
and it shows me:
它向我展示了:
ADMINISTRADOR DA ACENTUA??O
ADMINISTRADOR DA ACENTUA??O
I try now do it in my view file:
我现在尝试在我的视图文件中执行此操作:
{!!Auth::user()->name!!}
<?php die();
And this works fine. It shows me:
这工作正常。它告诉我:
ADMINISTRADOR DA ACENTUA??O
ADMINISTRADOR DA ACENTUA??O
It makes me believe the error occours for something laravel does after the views are parsed.
这让我相信在解析视图后 laravel 所做的事情会发生错误。
I don't know why it works well when I die the user name on the controller, but not works when I echo its name on the view.
我不知道为什么当我死在控制器上的用户名时它运行良好,但当我在视图上回显它的名称时不起作用。
May anyone help me plz?
有人可以帮我吗?
PS:
PS:
- My view file is using utf8 charset
- I tried to echo with and without html tags and charset meta. The problem occours on both cases
- I tried to delete my view file and create a new one with utf8 charset. It doesn't work.
- I tried to use
<?php echo Auth::user()->name; ?>
instead blade tags. It doesn't work.
- 我的视图文件使用的是 utf8 字符集
- 我尝试使用和不使用 html 标签和字符集元进行回显。两种情况都会出现问题
- 我试图删除我的视图文件并使用 utf8 字符集创建一个新文件。它不起作用。
- 我尝试使用
<?php echo Auth::user()->name; ?>
刀片标签代替。它不起作用。
采纳答案by Anderson Silva
I solved this issue using this aswer. I've just went in my AppServiceProvider and put into boot method:
我使用这个 aswer解决了这个问题。我刚刚进入我的 AppServiceProvider 并放入引导方法:
Blade::setEchoFormat('e(utf8_encode(%s))');
I don't know if this is the correct way to do it, but it works for me.
我不知道这是否是正确的方法,但它对我有用。
回答by Dave Amphlett
Generally sticking to UTF-8 keeps life simple.
通常坚持 UTF-8 让生活变得简单。
Be super careful copying and pasting from anywhere else into your code - basically always go through Notepad++ and use its convert to UTF-8 (without BOM) before copying and pasting into your code.
从其他任何地方复制和粘贴到您的代码中时要非常小心 - 在复制和粘贴到您的代码之前,基本上总是通过 Notepad++ 并使用其转换为 UTF-8(无 BOM)。
First question - how is the data getting into your database?
第一个问题 - 数据如何进入您的数据库?
Mega Top TipIf you're using a form, make sure you've set the accepted charset attribute on the form element:
Mega Top Tip如果您使用的是表单,请确保您已在表单元素上设置了接受的字符集属性:
<form accept-charset="UTF-8"
Then make sure all your views (including error pages), have
然后确保您的所有视图(包括错误页面)都具有
<meta charset="UTF-8">
Or the following if you're doing HTML4
或者,如果您正在使用 HTML4,请执行以下操作
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
回答by Fusion
I accidentally opened my UTF-8
encoded file in different text editor and saved. However, it automatically saved my UTF-8
file as ANSII
. So i got to open the file again and save file as UTF-8
encoded.
我不小心UTF-8
在不同的文本编辑器中打开了我的编码文件并保存了。但是,它自动将我的UTF-8
文件另存为ANSII
. 所以我必须再次打开文件并将文件保存为UTF-8
编码。
回答by Raffaele
Since successful and error response do not behave consistently, the difference may be in the HTTP headers. If that's the case, what happens is that Sqlserver and your browser use the same encoding, likely some Windows default, like cp1252, while Laravel is configured to respond with a different charset than the one used in the database.
由于成功响应和错误响应的行为不一致,因此不同之处可能在于 HTTP 标头。如果是这种情况,那么 Sqlserver 和您的浏览器会使用相同的编码,可能是某些 Windows 默认编码,例如 cp1252,而 Laravel 被配置为使用与数据库中使用的字符集不同的字符集进行响应。
Check the HTTP header content-type both when you invoke the view and when you die() - if that is your issue configure Laravel to send some header that agrees with your templates and your database encoding (or of course! transcode explicitely when needed)
在调用视图和 die() 时检查 HTTP 标头内容类型 - 如果这是您的问题,请配置 Laravel 发送一些与您的模板和数据库编码一致的标头(或者当然!在需要时显式转码)