首页 文章

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

提问于
浏览
17

如果找不到路线,我试图找出未找到404页面 . 我遵循了许多教程,但它不起作用 . 我_288340_在 \laravel\resources\views\errors

也在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,则返回空白页面 . 我有

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

在app.php中 .

如果找不到路线,任何人都可以帮我如何显示404页面?谢谢 .

6 回答

  • 5

    @ tester.Your问题已经解决,请在composer中尝试以下命令:

    php artisan view:clear
    

    然后使用未知的URL再次尝试 . 因为我以前也遇到过同样的错误 .

  • 0

    您无需检查错误类型并手动呈现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);
    }
    
  • 21

    enter image description here

    abort方法将立即引发异常处理程序呈现的异常 . (可选)您可以提供响应文本:

    abort(403, 'Unauthorized action.');
    

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

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

    我收到了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中的视图,依此类推 .

  • 3

    在apache中,您可以将此代码放在主目录的.htaccess文件中,并确保在httpd confg文件中将AllowOverride指令更改为all

    ErrorDocument 404 the\path\to\404.blade.php
    
  • 1

    我在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

相关问题