ThinkPHP项目错误页面定制

wen PHP项目 3

本文目录导读:

ThinkPHP项目错误页面定制

  1. ThinkPHP 6.0/8.0 版本(推荐)
  2. ThinkPHP 5.x 版本
  3. 使用路由和控制器方式(兼容所有版本)
  4. 注意事项

在ThinkPHP项目中定制错误页面,主要有以下几种方式,可以根据你的ThinkPHP版本选择合适的方法:

ThinkPHP 6.0/8.0 版本(推荐)

使用异常处理类

方式1:修改配置config/app.php 中添加:

return [
    // 其他配置...
    // 显示错误信息
    'show_error_msg' => true,
    // 异常页面模板
    'exception_tmpl' => app()->getRootPath() . 'view/error.tpl',
];

方式2:自定义异常处理类

app/ 目录下创建 exception/Handle.php

<?php
namespace app\exception;
use think\exception\Handle;
use think\Response;
use Throwable;
class Http extends Handle
{
    public function render($request, Throwable $e): Response
    {
        // 记录异常日志
        // 可以在这里根据状态码返回自定义页面
        // 如果是HTTP异常
        if ($e instanceof \think\exception\HttpException) {
            $statusCode = $e->getStatusCode();
            // 根据状态码返回不同模板
            if ($statusCode == 404) {
                return response(view('errors/404'), 404);
            } elseif ($statusCode == 500) {
                return response(view('errors/500'), 500);
            }
        }
        // 其他异常返回默认处理
        return parent::render($request, $e);
    }
}

创建错误页面模板

app/view/errors/ 目录下创建:

html:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">404 - 页面不存在</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            padding: 50px;
            background: #f5f5f5;
        }
        h1 {
            font-size: 72px;
            color: #ff6b6b;
            margin: 20px 0;
        }
        p {
            font-size: 18px;
            color: #666;
        }
        a {
            display: inline-block;
            margin-top: 20px;
            padding: 10px 20px;
            background: #4CAF50;
            color: white;
            text-decoration: none;
            border-radius: 5px;
        }
    </style>
</head>
<body>
    <h1>404</h1>
    <p>抱歉,您访问的页面不存在</p>
    <a href="/">返回首页</a>
</body>
</html>

html:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">500 - 服务器错误</title>
    <style>
        /* 类似404的样式 */
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            padding: 50px;
            background: #f5f5f5;
        }
        h1 {
            font-size: 72px;
            color: #ff6b6b;
            margin: 20px 0;
        }
        p {
            font-size: 18px;
            color: #666;
        }
        a {
            display: inline-block;
            margin-top: 20px;
            padding: 10px 20px;
            background: #4CAF50;
            color: white;
            text-decoration: none;
            border-radius: 5px;
        }
    </style>
</head>
<body>
    <h1>500</h1>
    <p>服务器出现错误,请稍后再试</p>
    <a href="/">返回首页</a>
</body>
</html>

ThinkPHP 5.x 版本

修改配置文件

application/config.php 中添加:

// 异常页面模板
'exception_tmpl' => THINK_PATH . 'tpl/think_exception.tpl',

自定义异常处理

application/ 目录下创建 common.php

<?php
// 自定义异常处理
\think\App::getInstance()->setExceptionHandler(function($e) {
    if ($e instanceof \think\exception\HttpException) {
        $statusCode = $e->getStatusCode();
        switch ($statusCode) {
            case 404:
                return response(view('errors/404'), 404);
                break;
            case 500:
                return response(view('errors/500'), 500);
                break;
            default:
                return response(view('errors/error'), $statusCode);
        }
    }
    // 其他异常
    return response('服务器错误', 500);
});

使用路由和控制器方式(兼容所有版本)

route/route.phpRoute.php 中添加:

Route::miss('Index/error');  // 404
Route::any('error/:code', 'Index/error');

在控制器中创建:

<?php
namespace app\controller;
use think\Controller;
class Index extends Controller
{
    public function error($code = 0)
    {
        $message = [
            '404' => '页面不存在',
            '500' => '服务器错误',
            '403' => '禁止访问',
        ];
        $this->assign('code', $code);
        $this->assign('message', $message[$code] ?? '未知错误');
        return $this->fetch('error');
    }
}

视图模板 view/error.html

<!DOCTYPE html>
<html>
<head>{$code} {$message}</title>
    <style>
        /* 自定义样式 */
    </style>
</head>
<body>
    <h1>{$code}</h1>
    <p>{$message}</p>
    <a href="/">返回首页</a>
</body>
</html>

注意事项

  1. 开发环境:建议开启详细错误信息,方便调试
  2. 生产环境:关闭详细错误,展示友好页面
  3. 日志记录:建议在错误页面中记录异常日志
  4. 安全考虑:不要在生产环境暴露错误详情

配置示例(生产环境):

// config/app.php (TP6)
return [
    'app_debug' => false,  // 关闭调试模式
    'show_error_msg' => true,  // 显示自定义错误
];

建议根据项目需求选择合适的方式,并确保错误页面样式与网站整体风格保持一致,提供友好的用户体验。

抱歉,评论功能暂时关闭!