更加完美的去設置錯誤頁面。
一、准備一個錯誤頁面 error.html,位置:thinkphp\template\index\default\error.html ,准備把前段所有的錯誤提示都指向這里。
二、空操作指向
在apps\index\controller\IndexBase.php,“基類”里面設置_empty
<?php /** * 前端基類 * */ namespace app\index\controller; use app\Common\controller\Base; class IndexBase extends Base { public function _initialize() { parent::_initialize(); } /** * 空操作 跳轉 * */ public function _empty(){ //abort(); exception(); // 這兩種方法都可以 } }
三、空控制器指向
在apps\index\controller\Error.php
<?php /** * 空控制器跳轉 * */ namespace app\index\controller; use app\index\controller; class Error extends IndexBase { public function index(){ abort(); } }
四、異常錯誤指向
在index/config.php exception_tmpl 參數。
'exception_tmpl' => THINK_PATH . 'tpl' . DS . 'think_exception.tpl', //'exception_tmpl' =>'E:/wamp/www/thinkphp/template/index/default/error.html',
注意:地址一定要絕對路徑。
拓展,
401,404,500等錯誤頁面自定義
相關參數:http_exception_template
手冊地址:http://www.kancloud.cn/manual/thinkphp5/163256
代碼:
config.php
'http_exception_template' => [ // 定義404錯誤的重定向頁面地址 404 => ROOT_PATH.config('template.view_path').config('index.model_name').'/'.config('index.default_template').'/404.html', // 還可以定義其它的HTTP status 401 => ROOT_PATH.config('template.view_path').config('index.model_name').'/'.config('index.default_template').'/401.html', ],
控制器調用
abort(404,'錯誤信息')
error.html,404.html 頁面代碼,可以參考thinkphp\thinkphp\tpl\think_exception.tpl