Thinkphp5笔记七:设置错误页面②

时间:2023-03-09 17:44:06
Thinkphp5笔记七:设置错误页面②

更加完美的去设置错误页面。

一、准备一个错误页面 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错误的重定向页面地址
=> ROOT_PATH.config('template.view_path').config('index.model_name').'/'.config('index.default_template').'/404.html',
// 还可以定义其它的HTTP status
=> 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