我想将 Laravel 版本 5.2 默认 404 页面重定向到我的 404 模板页面,从哪里更改以及更改什么

I want to redirect Laravel version 5.2 default 404 page to my template page of 404, from where and what to change

我正在使用 Laravel 5.2 版并且不知道如何将 Laravel 默认页面重定向到我的模板 404 页面

使用abort(404);

Some exceptions describe HTTP error codes from the server. For example, this may be a "page not found" error (404), an "unauthorized error" (401) or even a developer generated 500 error. In order to generate such a response from anywhere in your application, use the following:

abort(404);

如果你在你的路由或控制器中的任何地方调用 abort(404); 它会抛出 HTTPNotFoundException 它寻找一个 blade 模板显示在 resources/views/errors/ 目录中,文件名相同作为错误代码。

示例:

在你的 app/Http/routes.php

Route::get('/test', function(){
   return abort(404);
});

在您的 resources/views/errors/ 目录中创建 404.blade.php,注意文件名对应于 abort(404);

参考:https://laravel.com/docs/5.2/errors#http-exceptions

在/resources/views/errors目录下有一个404页面

编辑此页面,您可以使它看起来像您想要的那样

创建视图并在 app/Exceptions/Handler 中设置此代码。php :

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
.
.
.
public function render($request, Exception $exception)
{
    if($exception instanceof NotFoundHttpException)
    {
        return response()->view('missing', [], 404);
    }
    return parent::render($request, $e);
}

修改app/Exceptions/Handler.php,增加如下几行:-

public function render($request, Exception $e)
{
  if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) 
  {
    return redirect('/');
  } 
 return parent::render($request, $e); }

使用您的路线名称代替您要重定向的位置。