首页 > 解决方案 > 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);
    }
}

标签: phplaravel

解决方案


试试这个代码

if ($exception instanceof UnauthorizedException) {
    return response()->view('errors.403', [], 403);
}
return parent::render($request, $exception);

推荐阅读