如何在 laravel 8 中创建带有变量的自定义错误页面

How to create a custom error page with variables in laravel 8

提问人:Hilmi Hidayat 提问时间:6/10/2021 更新时间:1/27/2022 访问量:892

问:

大家早上好,如何在 laravel 8 中创建带有变量的自定义错误页面?因此,我想显示一个带有 @extend('layouts.app') 的错误页面,在此 layouts.app 中,我给出了一个变量,例如 $general 来显示 generals 表中的数据。

我已经尝试了下面的代码,但结果仍然是 Undefined variable: general

异常\处理程序.php

<?php

namespace App\Exceptions;

use App\Models\General;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
use Exception;
use Illuminate\Support\Arr;
use Illuminate\Auth\AuthenticationException;

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 = [
        'current_password',
        'password',
        'password_confirmation',
    ];

    /**
     * Register the exception handling callbacks for the application.
     *
     * @return void
     */
    public function register()
    {
        $this->reportable(function (Throwable $e) {
            //
        });
    }

    protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Unauthenticated.'], 401);
        }

        $guard = Arr::get($exception->guards(), 0);

        $route = 'login';

        if ($guard == 'admin') {
            $route = 'admin.login';
        }

        return redirect()->route($route);
    }

    public function render($request, Throwable $exception)
    {
        
        if($this->isHttpException($exception)){
            switch ($exception->getCode()) {
                case 404:
                    //return redirect()->route('404');
                    $general = General::find(1);
                    return response()->view('errors.404', ['general'   => $general], $exception->getCode());
                    break;
                case 405:
                    return response()->view('errors.405', [], $exception->getCode());
                    break;
                case 500:
                    return response()->view('errors.500', [], $exception->getCode());
                    break;
            }
        }        
        return parent::render($request, $exception);
    }
}

错误\404.刀片.php

@extends('layouts.front')

@section('title', __('Not Found'))
@section('code', '404')
@section('message', __('Not Found'))

Undefined variable: general

在 Laravel 8 错误页面中添加变量的正确方法是什么?谢谢:)

PHP 拉维尔

评论

0赞 STA 6/10/2021
这是一个 500 错误,而不是 404 错误。APP_DEBUG=false
0赞 Hilmi Hidayat 6/10/2021
我更改了 app_debug=false,但结果仍然相同

答:

0赞 Fendwyr 1/27/2022 #1

可以创建自己的服务提供程序(或仅使用 App\Providers\AppServiceProvider)

在引导方法中寄存 View::composer,它将侦听错误页面视图

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        // Using closure based composers...
        View::composer('errors::404', function ($view) {
            $general = General::find(1);
            $view->with('general', $general);
        });
    }

您也可以使用通配符“errors::*”