如果找不到路由,我想弄清楚如何显示 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,它会返回一个空白页面。我有
'debug' => env('APP_DEBUG', true),
在 app.php 中。
如果找不到路由,谁能帮我显示 404 页面?谢谢。
最佳答案
我收到了 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 等中的 View 。
关于php - 如果在 Laravel 5.1 中找不到路由,则显示 404 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32195346/