php Laravel 5 自定义 404

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26394017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:53:10  来源:igfitidea点击:

laravel 5 custom 404

phplaravellaravel-routinglaravel-5

提问by JNsites

This is driving me crazy. I'm working with Laravel 5 and it appears that the docs for 4.2 and generating 404 pages does not work.

这真让我抓狂。我正在使用 Laravel 5,似乎 4.2 和生成 404 页的文档不起作用。

First, there is no global.php so I tried putting the following in routes.php:

首先,没有 global.php,所以我尝试将以下内容放入 routes.php:

App::missing(function($exception)
{
    return Response::view('errors.missing', array(), 404);
});

This results in an error "method missing() not found"

这会导致错误“找不到方法丢失()”

Debug is set to false.

调试设置为假。

I've searched and searched but so far have found no information on setting 404 pages in Laravel 5. Would appreciate any help.

我已经搜索并搜索过,但到目前为止还没有找到有关在 Laravel 5 中设置 404 页的信息。希望得到任何帮助。

回答by JNsites

Go to resources/views/errors and create a 404.blade.php file with what you want on your 404 page and Laravel takes care of the rest.

转到资源/视图/错误并创建一个 404.blade.php 文件,其中包含您在 404 页面上想要的内容,Laravel 会负责其余的工作。

回答by Hayk Aghabekyan

if you want to have some global solution, you can do changes in /app/Exceptions/Handler.php by adding code bellow

如果你想有一些全局的解决方案,你可以通过添加下面的代码在 /app/Exceptions/Handler.php 中进行更改

public function render($request, Exception $e)
{
    if ($this->isHttpException($e)) {

        $statusCode = $e->getStatusCode();

        switch ($statusCode) {

            case '404':
                return response()->view('layouts/index', [
                    'content' => view('errors/404')
                ]);
        }
    }
    return parent::render($request, $e);
}

回答by Mithredate

In Laravel 5 you could simply put a custom 404.blade.phpunder resources/views/errorsand that's it. For other errors like 500 you could try the following in your app/Exeptions/Handler.php:

在 Laravel 5 中,您可以简单地将自定义404.blade.php放在资源/视图/错误下,就是这样。对于像 500 这样的其他错误,您可以在app/Exeptions/Handler.php 中尝试以下操作

public function render($request, Exception $e)
{

    if ( ! config('app.debug') && ! $this->isHttpException($e)) {
        return response()->view('errors.500');
    }

    return parent::render($request, $e);
}

And do the same for 500 HTTP Exeptions

并对 500 个 HTTP 例外执行相同操作

回答by jeremykenedy

I like the case statement approach but it has some issues going levels deep.

我喜欢 case statement 方法,但它有一些深入层次的问题。

However, this catches all errors:

但是,这会捕获所有错误:

Route::any('/{page?}',function(){
  return View::make('errors.404');
})->where('page','.*');

回答by Kent Aguilar

Laravel 5 already has a pre-defined render method(line 43) under app/Exceptions/Handler.php. Simply insert the redirection code before parent::render. Like so,

Laravel 5 在 app/Exceptions/Handler.php 下已经有一个预定义的渲染方法(第 43 行)。只需在 parent::render 之前插入重定向代码。像这样,

public function render($request, Exception $e)
{
    if ($e instanceof ModelNotFoundException) 
    {
        $e = new NotFoundHttpException($e->getMessage(), $e);
    }

    //insert this snippet
    if ($this->isHttpException($e)) 
    {
        $statusCode = $e->getStatusCode();
        switch ($statusCode) 
        {
            case '404': return response()->view('error', array(), 404);
        }
    }

    return parent::render($request, $e);
}

Note: My view is under resources/views. You can somehow put it anywhere else you want.

注意:我的视图位于资源/视图下。你可以以某种方式把它放在任何你想要的地方。

回答by stillatmylinux

Lavavel 5.8

拉瓦维尔 5.8

Create a file in resources/views/errors/404.blade.phpand add this code.

在其中创建一个文件resources/views/errors/404.blade.php并添加此代码。

@extends('errors::minimal')

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

@if($exception)
    @section('message', $exception->getMessage())
@else
    @section('message', __('Not Found'))
@endif

Then in your controller you can use:

然后在您的控制器中,您可以使用:

abort(404, 'Whatever you were looking for, look somewhere else');