这篇文章主要介绍了thinkphp5框架设置404、403等http状态页面的方法,结合实例形式分析了thinkphp5框架设置404页面的相关配置、视图显示页面及控制器调用相关操作技巧,需要的朋友可以参考下
本文实例讲述了thinkPHP5框架设置404、403等http状态页面的方法。分享给大家供大家参考,具体如下:
要做到这一点,首先要在你的配置文件将调试模式关闭(在开发阶段要打开):
'app_debug' => false,
然后在配置文件 config.php 中配置404等页面的模板路径(APP_PATH指的是application路径):
立即学习“PHP免费学习笔记(深入)”;
'http_exception_template' => [
// 定义404错误的重定向页面地址
404 => APP_PATH.'404.html',
// 还可以定义其它的HTTP status
401 => APP_PATH.'401.html',
403 => APP_PATH.'404.html',
],404页面位于 application 目录下,404.html部分代码如下:
<img src="__INDEX__/img/404.png" width="818" height="595" style="display: block;margin: 0 auto;">
<p class="" style="font-size: 36px;margin: 0 auto;text-align: center;color: #323232;">
您查找的页面不存在,还有
<span id="dd" style="color:darkorange;font-weight: bold;">6</span>
秒,页面将自动跳转首页...
</p>
<!--倒计时-->
<script type="text/javascript">
function run(){
var s = document.getElementById("dd");
if(s.innerHTML == 0){
window.location.href='/';
return false;
}
s.innerHTML = s.innerHTML * 1 - 1;
}
window.setInterval("run();", 1000);
</script>测试 controller
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 \think\exception\HttpException(403, '~~~非法请求~~~');
}404效果如下图:

相关推荐:
以上就是thinkPHP5框架设置404、403等http状态页面的方法的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号