如果在 Laravel 5.1 中找不到路由,则显示 404 页面

Show a 404 page if route not found in Laravel 5.1

我想弄清楚在找不到路由时显示 404 页面未找到。我遵循了很多教程,但它不起作用。 我在 \laravel\resources\views\errors

中有 404.blade.php

也在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);
}

如果我在浏览器中输入错误 URL,它会 returns 一个空白页面。我有

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

在 app.php.

如果找不到路由,谁能帮我显示 404 页面?谢谢。

> abort 方法将立即引发异常,该异常将由异常处理程序呈现。或者,您可以提供响应文本:

abort(403, 'Unauthorized action.');

你的 app_debug 设置为真了吗?如果是这种情况,Laravel 将通过回溯抛出错误以进行调试,如果将值更改为 false,Laravel 将在错误文件夹中显示默认的 404 页面。话虽如此,您可以随时选择使用中止。在控制器级别或路由级别,完全取决于您。

Route::get('/page/not/found',function($closure){
  // second parameter is optional. 
  abort(404,'Page not found');
  abort(403); 
});

在 apache 中,您可以将这段代码放在主目录的 .htaccess 文件中,并确保在 httpd 配置文件中将 AllowOverride Directive 更改为 all

ErrorDocument 404 the\path\to4.blade.php

您无需检查错误类型并手动渲染 404 视图。 Laravel 已经知道使用抛出的 HTTP 错误代码 (404 = resources/views/errors/404.blade.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"
        );
    }

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

@tester.Your 问题已经解决,在composer中试试下面的命令:

php artisan view:clear

然后再试一次未知 URL。因为我之前也遇到过同样的错误。

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

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

用这个函数替换函数:

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);
    }
}

然后您可以使用保存在 views/errors/404.blade.php 中的视图,依此类推。

我在 app/Exceptions/Handler.php (Laravel 5.2) 中使用了以下内容:

/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $e
 * @return \Illuminate\Http\Response
 */
public function render($request, Exception $e)
{
    if ($e instanceof \ReflectionException OR $e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) //Si la ruta no existe, mostar view 404.
        return response(view('errors.404'), 404);
    return parent::render($request, $e);
}

看起来像这样:img

在 config 文件夹中 app.php 更改以下代码

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

在 App->Exception->Handler.php 用下面的代码替换渲染函数

public function render($request, Exception $exception)
{
    
    if ($exception instanceof ModelNotFoundException) 
    {
    return response()->view('errors.404', [], 404);
    }

    
    if ($exception instanceof \ErrorException) {
    return response()->view('errors.500', [], 500);
    } 
    else {
    return parent::render($request, $exception);
    }
    return parent::render($request, $exception);
}