xvv/think-jump

ThinkPHP 8 jump/redirect trait package, compatible with PHP 8.4+

Maintainers

Package info

github.com/xvv1314159/think-jump

Language:HTML

pkg:composer/xvv/think-jump

Statistics

Installs: 19

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.3 2026-04-01 00:01 UTC

This package is auto-updated.

Last update: 2026-05-01 00:32:11 UTC


README

ThinkPHP 8 跳转与重定向扩展包,兼容 PHP 8.4+,内置现代美观的跳转页面模板,支持明暗主题自适应。

安装

composer require xvv/think-jump

使用

在控制器中引入并使用 Jump Trait:

<?php
namespace app\controller;

use xvv\think\Jump;

class Index extends BaseController
{
    use Jump;

    public function index()
    {
        // 操作成功,跳转到首页
        $this->success('登录成功', '/index/home');

        // 操作失败,返回上一页
        $this->error('用户名或密码错误');

        // AJAX 请求成功
        $this->success('保存成功', null, ['id' => 100]);

        // 直接重定向
        $this->redirect('/user/profile');

        // API 接口返回
        $this->result(['list' => []], 0, '查询成功');
    }
}

方法说明

success($msg, $url, $data, $wait, $header)

操作成功跳转。

参数 类型 默认值 说明
$msg mixed '' 提示信息
$url string|null null 跳转 URL,为 null 时返回上一页
$data mixed '' 返回数据
$wait int 3 等待秒数
$header array [] 自定义响应头

error($msg, $url, $data, $wait, $header)

操作失败跳转。$urlnull 时:AJAX 请求返回空字符串,普通请求返回 javascript:history.back(-1)

result($data, $code, $msg, $type, $header)

直接封装并返回 API 数据,不走页面跳转。

参数 类型 默认值 说明
$data mixed 返回数据
$code int 0 状态码
$msg mixed '' 提示信息
$type string '' 响应类型,为空时自动判断
$header array [] 自定义响应头

返回数据格式:

{
    "code": 0,
    "msg": "查询成功",
    "time": 1743475200,
    "data": [...]
}

redirect($url, $code, $with)

HTTP 重定向。

参数 类型 默认值 说明
$url string '' 目标 URL
$code int 302 HTTP 状态码(301/302/307/308)
$with array [] 通过 Session 附加的参数

配置

发布配置文件:

php think service:discover
php think vendor:publish

或在 config/ 目录下手动创建 jump.php

<?php
// config/jump.php
return [
    // 成功跳转页模板路径
    'dispatch_success_tpl' => app()->getRootPath() . '/vendor/xvv/think-jump/src/tpl/success.html',
    // 失败跳转页模板路径
    'dispatch_error_tpl'   => app()->getRootPath() . '/vendor/xvv/think-jump/src/tpl/error.html',

    // 成功响应 code 值
    'default_success_code' => 0,
    // 失败响应 code 值
    'default_error_code'   => 1,

    // 成功跳转等待秒数
    'default_success_wait' => 3,
    // 失败跳转等待秒数
    'default_error_wait'   => 3,

    // 默认输出类型(非 AJAX 请求)
    'default_return_type'  => 'html',
    // AJAX 请求默认返回格式,支持 json / jsonp / xml
    'default_ajax_return'  => 'json',
];

模板说明

内置跳转模板 success.htmlerror.html 支持:

  • 响应式布局,适配移动端
  • 明暗主题自动切换(跟随系统偏好)
  • 自动倒计时跳转
  • 支持自定义模板路径覆盖默认配置

进阶用法

路由 URL 构建

传入非绝对路径时,自动调用 $this->app->route->buildUrl() 构建路由 URL:

$this->success('更新成功', 'user/profile');  // 自动构建为 /user/profile

自动判断响应类型

框架自动根据请求类型选择响应格式:

  • AJAX / JSON 请求 → 返回 JSON 数据
  • 普通请求 → 返回 HTML 跳转页

环境要求

  • PHP >= 8.0
  • ThinkPHP >= 8.0
  • topthink/think-view >= 2.0

License

Apache-2.0