Laravel 5 charset not working correctly on the vie

2019-01-19 15:22发布

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:

{!!Auth::user()->name!!} 

it shows:

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

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

It makes me believe the error occours for something laravel does after the views are parsed.

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:

  • 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.

4条回答
走好不送
2楼-- · 2019-01-19 15:45

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.

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)

查看更多
【Aperson】
3楼-- · 2019-01-19 15:53

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.

查看更多
▲ chillily
4楼-- · 2019-01-19 15:59

Generally sticking to UTF-8 keeps life simple.

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.

First question - how is the data getting into your database?

Mega Top Tip If you're using a form, make sure you've set the accepted charset attribute on the form element:

<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

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
查看更多
Fickle 薄情
5楼-- · 2019-01-19 16:02

I solved this issue using this aswer. I've just went in my AppServiceProvider and put into boot method:

Blade::setEchoFormat('e(utf8_encode(%s))');

I don't know if this is the correct way to do it, but it works for me.

查看更多
登录 后发表回答