thinkPHP5框架设置404、403等http状态页面的方法
author:一佰互联 2019-04-25   click:187

本文实例讲述了thinkPHP5框架设置404、403等http状态页面的方法。分享给大家供大家参考,具体如下:

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

"app_debug" => false,

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

"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;"><div class="" style="font-size: 36px;margin: 0 auto;text-align: center;color: #323232;">  您查找的页面不存在,还有  <span id="dd" style="color:darkorange;font-weight: bold;">6</span>  秒,页面将自动跳转首页...</div><!--倒计时--><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 	hinkexceptionHttpException(403, "~~~非法请求~~~");}

404效果如下图:

更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《Zend FrameWork框架入门教程》及《PHP模板技术总结》。

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。