如何在laravel 5中创建自定义错误页面

Kam*_*esh 2 php laravel-5

我想在我的laravel 5 app中显示自定义错误页面.例如任何用户类型网址如http://www.app.com/url123(错误)但http://www.app.com/url(右)

默认错误显示为:

呃哦,出了点问题!错误代码:500

但相反,我想显示我的自定义视图

我怎么能这样做:我喜欢的一些链接但尚未实现

https://mattstauffer.co/blog/laravel-5.0-custom-error-pages#how-to

https://laracasts.com/discuss/channels/general-discussion/how-do-i-create-a-custom-404-error-page

我目前的app/Exceptions/Handler.php:

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use symfony\http-kernel\Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class Handler extends ExceptionHandler {

    protected $dontReport = [
        'Symfony\Component\HttpKernel\Exception\HttpException'
    ];

    public function report(Exception $e)
    {
        return parent::report($e);
    }
    public function render($request, Exception $e)
    {
        if ($this->isHttpException($e))
        {
            return $this->renderHttpException($e);
        }
        else if($e instanceof NotFoundHttpException)
        {
            return response()->view('missing', [], 404);
        }
        else
        {
            return parent::render($request, $e);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

我在:\ resources\views\errors\404.blade.php上创建了一个错误视图

但仍然没有加载我的404.blade.php视图

Sof*_*per 7

我使用Laravel 6.4,为此,您可以创建自定义错误页面刀片文件,Laravel 将为您完成剩下的工作。在您的项目目录中运行以下命令

php artisan vendor:publish --tag=laravel-errors
Run Code Online (Sandbox Code Playgroud)

这将在resources/views/errors/.例如 404 错误中创建刀片文件,您将在resources/views/errors/404.blade.php.. 现在假设您希望在示例代码中传递Not Found Error假设一个返回帖子类别的函数

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
    $count = Post::find($id)->count();

    if ($count < 1)  return abort('404', 'The post you are looking for was not found');
}
Run Code Online (Sandbox Code Playgroud)

abort()方法将触发未找到异常加载刀片文件resources/views/errors/404.blade.php并传入第二个参数作为消息。您可以在刀片文件中访问此消息

<h2>{{ $exception->getMessage() }}</h2>
Run Code Online (Sandbox Code Playgroud)

转到此链接(官方文档)了解详情https://laravel.com/docs/6.x/errors#custom-http-error-pages


Kam*_*esh 5

谢谢你们,现在它成功了,

我只是更改了我的 app/Exceptions/Handler.php:

<?php namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler {

    protected $dontReport = [
        'Symfony\Component\HttpKernel\Exception\HttpException'
    ];

    public function report(Exception $e)
    {
        return parent::report($e);
    }
    public function render($request, Exception $e)
    {
        if ($this->isHttpException($e))
        {
            return $this->renderHttpException($e);
        }
        else
        {
            return parent::render($request, $e);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

并在:\ resources\views\errors\404.blade.php上创建一个错误视图

  • 要完成您的答案,如果您不想要默认错误页面(Whoops ...)而不是返回`parent :: render`,您可以返回响应对象`response() - > view('my.custom.view ',[],500); ` (2认同)