我有你同樣的問題,解決它添加一箇中間件它取代UTF-8與ISO-8859-1的內容類型字符集頭。
我創建了一個新的中間件類,命名爲CharsetMiddleware
,這些內容:
namespace App\Http\Middleware;
use Closure;
class CharsetMiddleware
{
public function handle($request, Closure $next)
{
$response = $next($request);
$contentType = $response->headers->get('Content-Type');
if (!empty($contentType)) {
$response->header('Content-Type', str_replace('UTF-8', 'iso-8859-1', $contentType));
}
return $response;
}
}
我把它放在應用程序/ HTTP /中間件/文件夾,然後在應用程序/ HTTP/Kernel.php類在添加陣列$middleware
屬性的端部的類引用,像這樣:
protected $middleware = [
// ... Other middleware references
\App\Http\Middleware\CharsetMiddleware::class,
];
的字符集適用於:數據庫客戶端,數據庫服務器,文件內容,文件名,請求流,響應流。你必須對你想要改變的東西做更具體的描述。 – apokryfos
您可以驗證您的瀏覽器是否正確接收標題?例如。打開Chrome開發者控制檯,打開Network選項卡,點擊'/ notificacao/nova'請求,並驗證'Content-Type'頭。 – Pete
指定標題上的字符集隻影響瀏覽器將如何讀取內容。如果你的數據在數據庫或者你的代碼上是UTF-8,你也需要把它轉換成ISO。 –