在 Laravel 中,当在中间件中响应 404 时,报错:View [components.css] not found
1、在 Laravel 中,当在中间件中响应 404 时,报错:View [components.css] not found。如图1
View [components.css] not found. (View: /var/www/object/resources/views/view_object/layouts/basic.blade.php) (View: /var/www/object/resources/views/view_object/layouts/basic.blade.php) {"exception":"[object] (Illuminate\\View\\ViewException(code: 0): View [components.css] not found. (View: /var/www/object/resources/views/view_object/layouts/basic.blade.php) (View: /var/www/object/resources/views/view_object/layouts/basic.blade.php) at /var/www/object/vendor/laravel/framework/src/Illuminate/View/FileViewFinder.php:137) [stacktrace] #0 /var/www/object/vendor/laravel/framework/src/Illuminate/View/Engines/PhpEngine.php(45): Illuminate\\View\\Engines\\CompilerEngine->handleViewException(Object(Illuminate\\View\\ViewException), 0) #1 /var/www/object/vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php(59): Illuminate\\View\\Engines\\PhpEngine->evaluatePath('/var/www/object/...', Array) #2 /var/www/object/vendor/laravel/framework/src/Illuminate/View/View.php(143): Illuminate\\View\\Engines\\CompilerEng...
2、查看对应主题中,确认是存在文件 /resources/views/view_object/components/css.blade.php 的。如图2
3、同样的响应 404 的代码,在中间件中报错:View [components.css] not found 。在控制器方法中响应 404。分别打印主题ID,最终确认主题ID在中间件中为空,当查找 components.css 时,会查找:/resources/views/components/css.blade.php,因为找不到,进而报错。如图3
var_dump(\Igaster\LaravelTheme\Facades\Theme::get()); exit; return response()->view('errors.404', [], 404);
string(6) "" string(6) "view_object"
4、在中间件中响应 404 之前,需要先设置主题ID,执行 /vendor/igaster/laravel-theme/src/Themes.php 的 set()。不再报错,符合预期。如图4
近期评论