• thinkphp5设置403 404等http状态页面


    在thinkphp5中如何抛出异常状态码(比如401,403,404等),因为这些能极大的给用户以良好的体验。

    因为在上线阶段,任何的系统错误信息都不能让浏览用户给看到,比如404(Not Found)页面我们应该直接抛出一个404异常,最好是配合一个404页面来展示出来,给用户以最好的体验,这是非常重要的。

    要做到这一点,首先要在你的配置文件将调试模式关闭(在开发阶段要打开):

    1
    'app_debug'              => false,

    然后在配置文件中配置404等页面的模板路径(APP_PATH指的是application路径):

    1
    'http_exception_template'    =>  [    404 =>  APP_PATH.'404.html',    403 =>  APP_PATH.'404.html',]

    404页面部分代码如下:

    1
    2
    3
    4
    5
    6
    7
    <div class="bg">   
    <div class="cont">      
    <div class="c1"><img src="/public/static/404/01.png" class="img1" /></div>      
    <h2><?php echo $e->getMessage()?><!--输出抛出异常信息--></h2>      
    <div class="c2"><a href="#" class="re">返回论坛</a><a href="#" class="home">网站首页</a><a href="#" class="sr">搜索一下页面相关信息</a></div>      
    <div class="c3">您可能输入了错误的网址,或者该网页已删除或移动,千锋PHP</div>   
    </div></div>

    下面来进行测试:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    if (Request::instance()->isAjax()) {    
        $data = input();    
        $info = [];    
        $where '';    
        switch ($data['msg']) {        
        case '验证码':            
            $info = [                
            'y' => '输入正确',                
            'n' => '输入错误',            
            ]; 
                
        $where = session::get('admin_login_session') == md5($data['param']);break;    
      }    
      if ($where) {        
        echo '{"info":"' $data['msg'] . $info ['y'] . '","status":"y"}';//注意ValidForm返回格式(json)   
      else {        
        echo '{"info":"' $data['msg'] . $info ['n'] . '","status":"n"}';//注意ValidForm返回格式(json)    
      }
     }else{    
     throw new  hinkexceptionHttpException(403, '~~~千锋PHP通知您非法请求~~~');    //因为此处只能是ajax来访问,当直接在浏览器中访问该方法时,
     可以抛出一个403,其他类似),此处有简写方法abort代替
     }
  • 相关阅读:
    js 置顶操作
    js input输入数量控制
    js 时间倒计时
    html内容垂直居中
    大图片随浏览器水平居中显示
    img,display:inline相关间隙样式问题
    js淡入淡出轮换思想(1)
    js 禁止|阻止滚动条滚动
    kotlin学习--第一个kotlin项目
    jdk8+Mybatis3.5.0+Mysql读取LongBlob失败
  • 原文地址:https://www.cnblogs.com/gaohj/p/7015889.html
Copyright © 2020-2023  润新知