我试图找出显示404页未找到,如果没有找到路由.我听了很多教程,但都不管用.

同样在handler中.php

public function render($request, Exception $e)
{
    if ($e instanceof TokenMismatchException) {
        // redirect to form an example of how i handle mine
        return redirect($request->fullUrl())->with(
            'csrf_error',
            "Opps! Seems you couldn't submit form for a longtime. Please try again"
        );
    }

    /*if ($e instanceof CustomException) {
        return response()->view('errors.404', [], 500);
    }*/

    if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
        return response(view('error.404'), 404);

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

If I enter wrong URL in browser, it returns a blank page. I have

'debug' => env('APP_DEBUG', true),

in app.php.

如果找不到路由,有人能帮我怎么显示404页面吗?非常感谢.

推荐答案

我收到了500个错误,而不是404个错误.我是这样解决问题的:

在app/Exceptions/Handler.php文件中,有一个render函数.

Replace the function with this function:

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

            // not authorized
            case '403':
                return \Response::view('errors.403',array(),403);
                break;

            // not found
            case '404':
                return \Response::view('errors.404',array(),404);
                break;

            // internal error
            case '500':
                return \Response::view('errors.500',array(),500);
                break;

            default:
                return $this->renderHttpException($e);
                break;
        }
    } else {
        return parent::render($request, $e);
    }
}

You can then use views that you save in views/errors/404.blade.php, and so on.

Laravel相关问答推荐

URL类图中的循环关系

未将Laravel Blade属性传递给组件或类

Laravel FAKER语法

我在 laravel 中插入多个复选框值.我怎样才能做到这一点?

Laravel 5 - 更改模型文件位置

Laravel 如何处理来自浏览器的 PUT 请求?

Laravel:使用 Input::old 时 textarea 不会重新填充

Laravel + SQLite = SQLSTATE[HY000]一般错误:8次try 写入只读数据库

Laravel 5.3 创建模型返回字段没有默认值

实施 Payum/Laravel 定期付款

Eloquent 的集合方法,例如 only 或 except 返回一个空集合

Laravel 合集日期比较

如何在 Laravel 中创建类别的嵌套列表?

Laravel Blade - 如果满足条件,则添加一个类

如何在 php Laravel 中对关联数组进行排序

Laravel Eloquent - 加入表时防止覆盖值

Laravel 迁移 - 删除列

如何在不使用视图的情况下使用 Laravel 4 发送Electron邮件?

将 Laravel 集合排序为 ID 数组

如何在 Laravel 中 Refresh刷新用户对象?