php - Laravel 目标类 [PagesController] 处理程序中不存在错误
问题描述
我的最终目标是使用数据库中的数据显示在 404 页面上。
我在 render 方法中将以下代码添加到我的 Handler.php 中:
if ($this->isHttpException($exception)) {
if ($exception->getStatusCode() == 404) {
return response()->view('errors.' . '404', [], 404);
}
}
errors.404.blade.php 的内容只是“测试”。
我检查了 PagesController 文件的名称,它应该是大写的。我还尝试使用Artisan::call('route:clear');
、Artisan::call('cache:clear');
、Artisan::call('cache:clear');
和清空缓存Artisan::call('view:clear');
(我无法直接访问终端)。
任何帮助表示赞赏。
谢谢。
编辑:这是完整的处理程序文件:
<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Support\Facades\App;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];
/**
* Report or log an exception.
*
* @param \Exception $exception
* @return void
*
* @throws \Exception
*/
public function report(Exception $exception)
{
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Exception
*/
public function render($request, Exception $exception)
{
if ($this->isHttpException($exception)) {
if ($exception->getStatusCode() == 404) {
return response()->view('errors.' . '404', [], 404);
}
}
return parent::render($request, $exception);
}
}
解决方案
试试这个代码
if ($exception instanceof UnauthorizedException) {
return response()->view('errors.403', [], 403);
}
return parent::render($request, $exception);
推荐阅读
- c# - 如何在给定范围内反向打印数组元素
- r - 使用 R 中的 Statnet 在加权网络中测量中心性
- javascript - 关于 JS 数据类型 Null
- precision - 何时为 IEEE 浮点数保证 (x==(x+y)-y) 或 (x==(xy)+y)?
- spring-boot - 我可以在控制器中使用 `@Cacheable` 吗?
- php - 如何在 php 中编写 JSON_TABLE 查询?
- amazon-web-services - 新的 S3 存储桶创建了托管静态网站:访问被拒绝/403 禁止错误
- c++ - C ++模板函数 - 多种类型,默认和...参数?
- reactjs - react-custom-scrollbars 包工作示例打字稿反应
- ruby-on-rails - Ruby On Rails Active Storage 清除/删除方法抛出 NoMethodError