How do I get Laravel to specify my charset in all

2019-08-15 03:41发布

问题:

Yes, I know I should be using UTF-8, but I need to use windows-1252 charset encoding.

I know I can do it by hardcoding the base Synfony response class Response.php,

$charset = $this->charset ?: 'windows-1252';

but that's ugly as hell.

I can't find where to set it from the config files. Any help?

回答1:

You can change charset in the middleware:

<?php namespace App\Http\Middleware;

use Closure;

class SetCharset
{
    public function handle($request, Closure $next)
    {
        $response = $next($request);
        $response->header('Content-Type', 'text/html; charset=windows-1252');

        return $response;
    }
}

Just make sure that all the content you return is in proper encoding.