Laravel5.5+框架中配置自定义404错误页面流程步骤
今天简单的配置一下laravel5.8框架的404页面,我这区分了一下pc端跟m端各自跳自己的已经定义好的404页面,所以记录一下供大家查阅.
在Laravel框架中所有的异常都由Handler类处理,该类包含两个方法:report和render,其中render方法将异常渲染到http响应中。
laravel的Handler类文件位置:app/Exceptions/Handler,由于render方法时间异常渲染到http响应中,所以我们只需要修改下render方法即可
网上很多的方法是将render方法修改成:
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
//自定义404页面路径,文件名 s
if ($this->isHttpException($exception)) {
if ($exception instanceof NotFoundHttpException) {
$host = explode('.',$request->server()['HTTP_HOST']);
//区分pc m
if ($host[0] == 'm' or $host[0] == 'mtest') {
return response()->view('errors.m404', [], 404);
}else{
return response()->view('errors.404', [], 404);
}
}
return $this->renderHttpException($exception);
}
//end
return parent::render($request, $exception);
}
404模板位置
/resources/views/errors/
模板我就简单写写
m404.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>m404</title>
<meta name="keywords" content="m404">
<meta name="description" content="m404">
</head>
<body>
<section>
<p><img src=""></p>
<p>很抱歉,您要访问的页面不存在或已被删除!</p>
<p><a href="https://www.zongscan.com/">返回首页</a></p>
</section>
<section>
<ul class="class-nav classnav">
<li><a href="https://www.zongscan.com/">侯体宗的博客</a></li>
<li><a href="https://blog.zongscan.com/">技术博客集</a></li>
</ul>
</section>
</body>
</html>
404.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>404</title>
<meta name="keywords" content="404">
<meta name="description" content="404">
</head>
<body>
<section>
<p><img src=""></p>
<p>很抱歉,您要访问的页面不存在或已被删除!</p>
<p><a href="https://www.zongscan.com/">返回首页</a></p>
</section>
<section>
<ul class="class-nav classnav">
<li><a href="https://www.zongscan.com/">侯体宗的博客</a></li>
<li><a href="https://blog.zongscan.com/">技术博客集</a></li>
</ul>
</section>
</body>
</html>
完
相关文章