本文實例講述了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 \think\exception\HttpException(403, '~~~非法請求~~~'); }
404效果如下圖:
希望本文所述對大家基于ThinkPHP框架的PHP程序設計有所幫助。