TP3.2自定义错误页面、成功页面及异常页面

ThinkPHP 作为一款轻量级PHP开发框架,它为我们提供了自带的错误页面、异常页面等信息提示页面,比如下面的代码会出现这样的提示:
$this->error('验证码错误!');

我们可以看到,自带的页面并不美观,所以我们需要自定义这些页面,ThinkPHP 为我们提供了自定义提示页面这样一种功能。

在配置文件中添加如下配置信息:

/* 错误页面模板 */
'TMPL_ACTION_ERROR'     =>  MODULE_PATH.'View/Public/error.html', // 默认错误跳转对应的模板文件 根据自己实际存放页面的路径写就可以
'TMPL_ACTION_SUCCESS'   =>  MODULE_PATH.'View/Public/success.html', // 默认成功跳转对应的模板文件
'TMPL_EXCEPTION_FILE'   =>  MODULE_PATH.'View/Public/exception.html',// 异常页面的模板文件

把这段配置信息放入到当前模型的 /Conf/config.php 中,然后在当前模型的 View 目录下创建 Public 文件夹,并在里面自定义 error.html success.html 及 exception.html 即可。

下面是一个简单的错误页面模板:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>跳转提示</title>
<style type="text/css">
*{ padding: 0; margin: 0; }
body{ background: #290C0C; font-family: '微软雅黑'; color: #fff; font-size: 16px; }
.system-message{ padding: 24px 48px; }
.system-message h1{ font-size: 80px; font-weight: normal; line-height: 120px; margin-bottom: 12px }
.system-message .jump{ padding-top: 10px;margin-bottom:20px}
.system-message .jump a{ color: #333;}
.system-message .success,.system-message .error{ line-height: 1.8em; font-size: 36px }
.system-message .detail{ font-size: 12px; line-height: 20px; margin-top: 12px; display:none}
#wait {
    font-size:46px;
}
#btn-stop,#href{
    display: inline-block;
    margin-right: 10px;
    font-size: 16px;
    line-height: 18px;
    text-align: center;
    vertical-align: middle;
    cursor: pointer;
    border: 0 none;
    background-color: #8B0000;
    padding: 10px 20px;
    color: #fff;
    font-weight: bold;
    border-color: transparent;
    text-decoration:none;
}
 
#btn-stop:hover,#href:hover{
    background-color: #ff0000;
}
</style>
</head>
<body>
<div class="system-message">
<h1>抱歉,出错啦!</h1>
<p class="error"><?php echo($error); ?></p>
<p class="detail"></p>
<p class="jump">
<b id="wait"><?php echo($waitSecond); ?></b> 秒后页面将自动跳转
</p>
<div>
    <a id="href" id="btn-now" href="<?php echo($jumpUrl); ?>">立即跳转</a> 
    <button id="btn-stop" type="button" onclick="stop()">停止跳转</button> 
    <a id="href" id="btn-now" href="<?php echo(U('Public/logout')); ?>">重新登录</a> 
</div>
</div>
<script type="text/javascript">
(function(){
 var wait = document.getElementById('wait'),href = document.getElementById('href').href;
 var interval = setInterval(function(){
        var time = --wait.innerHTML;
        if(time <= 0) {
            location.href = href;
            clearInterval(interval);
        };
     }, 1000);
  window.stop = function (){
         console.log(111);
            clearInterval(interval);
 }
 })();
</script>
</body>
</html>

我们把上面的代码放到自定义 error.html 页面中,然后再看最开始的错误提示页面就会变成下面这样:

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,918评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,874评论 25 708
  • 老友最近天天折磨我的神经 如果有一天我去四院了 那一定是老友把我逼疯的 最近老友疯狂的爱着一个人(下文开始以货代称...
    杨小小羊阅读 218评论 22 1
  • 晚睡,早起,送懂事长去上学。 打开混沌大学APP,快看漫画陈安妮分享运营心得,剖析年轻人的秘密,一组惊人的数字背后...
    械密阅读 280评论 1 3
  • 上午,学校新来了家长带来特殊的孩子,听说普通小学学校教师不喜欢他不要他,他学什么都不知道,连一年级都不会认字!...
    b26b2e4a1f9b阅读 206评论 0 0

友情链接更多精彩内容