Laravel 9渲染核心404 Blade时抛出htmlspecialchars异常求助
Laravel 9生产环境404模板渲染抛出htmlspecialchars类型错误
问题详情
Laravel 9站点在生产环境渲染核心404 Blade模板时抛出异常,异常信息:
htmlspecialchars(): Argument #1 ($string) must be of type string, _ given
异常路径指向/var/app/current/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/views/404.blade.php。本地环境使用相同PHP 8.1.22版本,无此问题。
自定义404.blade.php代码
@extends('errors::minimal') @section('title', __('Not Found')) @section('code', '404') @section('message', __('Not Found'))
临时解决方法
将__('Not Found')替换为直接字符串'Not Found'后,异常不再抛出,404页面可正常渲染。
异常追踪信息
#0 /var/app/current/vendor/laravel/framework/src/Illuminate/Support/helpers.php(123): htmlspecialchars(Object(_), 3, 'UTF-8', true) #1 /var/app/current/vendor/laravel/framework/src/Illuminate/View/Concerns/ManagesLayouts.php(53): e(Object(_)) #2 /var/app/current/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/views/404.blade.php(3): Illuminate\View\Factory->startSection('title', Object(_)) #3 /var/app/current/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php(109): require('/var/app/curren...') #4 /var/app/current/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php(110): Illuminate\Filesystem\Filesystem::Illuminate\Filesystem\{closure}() #5 /var/app/current/vendor/laravel/framework/src/Illuminate/View/Engines/PhpEngine.php(58): Illuminate\Filesystem\Filesystem->getRequire('/var/app/curren...', Array)
排查与解决方案
- 检查语言文件完整性:确认生产环境
lang目录下对应语言的翻译文件(如en.json或en/validation.php等)中存在"Not Found"的翻译条目,缺失会导致__()函数返回非字符串对象。 - 清除视图缓存:执行生产环境命令
php artisan view:clear,清除编译后的旧视图文件,避免缓存导致的渲染异常。 - 验证语言配置:确认
.env文件中APP_LOCALE设置正确,且对应语言目录存在完整的翻译资源。 - 调试翻译返回值:临时在404模板中添加
{{ var_dump(__('Not Found')) }},查看返回值类型,定位翻译流程是否出错。
内容的提问来源于stack exchange,提问作者bfuzze
相关产品推荐
相关产品推荐

