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

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

一、准备一个错误页面 error.html,位置:thinkphp emplateindexdefaulterror.html ,准备把前段所有的错误提示都指向这里。

二、空操作指向

在appsindexcontrollerIndexBase.php,“基类”里面设置_empty

<?php
/**
 * 前端基类
 * */
namespace appindexcontroller;
use  appCommoncontrollerBase;

class IndexBase extends  Base
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 空操作 跳转
     * */
    public function _empty(){
        //abort();     
        exception();     //  这两种方法都可以
    }


}

三、空控制器指向

在appsindexcontrollerError.php

<?php
/**
 * 空控制器跳转
 * */
namespace appindexcontroller;
use appindexcontroller;

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 hinkphp pl hink_exception.tpl

原文地址:https://www.cnblogs.com/wesky/p/6815416.html