侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Laravel5.5+框架中配置自定义404错误页面流程步骤

Laravel  /  管理员 发布于 1年前   364

今天简单的配置一下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>



完



  • 上一条:
    CentOS如何安装php?
    下一条:
    .NET Core 微信小程序退款步骤――(统一退款)
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel 10.4版本发布(0个评论)
    • 在laravel框架中的5个HTTP客户端技巧分享(0个评论)
    • 在laravel项目中实现密码强度验证功能推荐扩展包:password-strength(0个评论)
    • Laravel Response Classes 响应类使用优化浅析(0个评论)
    • 在Laravel中使用FilePond上传文件及测试用例(0个评论)
    • 近期文章
    • 在go语言中使用GoPDF包把html生成PDF文件示例(0个评论)
    • 在go语言中创建和解析(读取)符号链接示例(0个评论)
    • ubuntu 22.04系统中报错:Python 3.6 is no longer supported by the Python core team...解决方式(0个评论)
    • Laravel 10.4版本发布(0个评论)
    • mysql5.7中实现分区表及分区where in查询示例及分区分表对比浅析(0个评论)
    • nginx + vue配置实现同域名下不同路径访问不同项目(0个评论)
    • 在laravel框架中的5个HTTP客户端技巧分享(0个评论)
    • 在go语言中使用FFmpeg库实现PCM音频文件编码为mp3格式文件流程步骤(0个评论)
    • gopacket免安装Pcap实现驱动层流量抓包流程步骤(0个评论)
    • 在laravel项目中实现密码强度验证功能推荐扩展包:password-strength(0个评论)
    • 近期评论
    • 博主 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..
    • xiaoB 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..
    • BUG4 在

      你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..
    • 博主 在

      go语言+beego框架中获取get,post请求的所有参数中评论 @ t1  直接在router.go文件中配就ok..
    • Jade 在

      如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-01
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客