initearth / think-jump
适用于 ThinkPHP 8.1 / PHP 8.4 的跳转扩展
v3.0.0
2026-12-03 02:19 UTC
Requires
- php: ^8.4
- topthink/framework: ^8.1
- topthink/think-view: ^2.0
Requires (Dev)
- mockery/mockery: 1.6.*
- pestphp/pest: ^3.8
- phpstan/phpstan: ^2.1
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
面向 ThinkPHP 8.1+ / PHP 8.4+ 的跳转扩展。通过 trait 或门面提供 success()、error()、result()、redirect(),统一处理 HTML 页面跳转与 API 响应。
特性
- 双入口:控制器
use Jumptrait,或使用Initearth\ThinkJump\facade\Jump门面。 - 自动协商:普通请求渲染跳转模板,Ajax / JSON 请求返回 JSON。
- 统一输出:
result()支持 JSON / JSONP / XML。 - 安全默认:过滤危险协议、协议相对地址与控制字符;模板变量自动转义;Referer 默认只接受同源。
- 无状态设计:依赖通过构造函数注入,适配 Think-Swoole 常驻内存。
环境要求
| 依赖 | 版本 |
|---|---|
| PHP | ^8.4 |
| topthink/framework | ^8.1 |
| topthink/think-view | ^2.0 |
安装
composer require initearth/think-jump php think service:discover
service:discover 注册扩展服务,绑定 JumpInterface 与 Clock。可选发布配置到 config/jump.php:
php think jump:publish # 加 --force 可覆盖已有配置
不发布配置也能直接使用内置默认值。
快速开始
控制器 trait
<?php namespace app\controller; use Initearth\ThinkJump\Jump; class Index { use Jump; public function save() { return $this->success('保存成功', 'index/index', ['id' => 1]); } }
门面
use Initearth\ThinkJump\facade\Jump; Jump::error('操作失败', 'login/index');
依赖注入
use Initearth\ThinkJump\contract\JumpInterface; class Index { public function __construct(private readonly JumpInterface $jump) { } public function save() { return $this->jump->success('保存成功', 'index/index'); } }
四个方法内部都会抛出 think\exception\HttpResponseException,由 ThinkPHP 接管响应;调用后的代码不会继续执行。
配置
config/jump.php:
<?php return [ // 模板路径不存在时自动回退到内置模板 'dispatch_success_tmpl' => \Initearth\ThinkJump\jump\JumpTemplate::default(), 'dispatch_error_tmpl' => \Initearth\ThinkJump\jump\JumpTemplate::default(), // success() 未指定地址且 Referer 不可用时的回退地址;'' 表示不跳转 'default_url' => '/', // Referer 白名单域名;为空时只接受同源 'allowed_referer_hosts' => [], ];
内置模板见 src/tpl/dispatch_jump.html。
方法
success(string|int|float|bool|array|object|null $msg = '', ?string $url = null, string|int|float|bool|array|object|null $data = '', int $wait = 3, array $header = []): never error(string|int|float|bool|array|object|null $msg = '', ?string $url = null, string|int|float|bool|array|object|null $data = '', int $wait = 3, array $header = []): never result(string|int|float|bool|array|object|null $data, int $code = 0, string|int|float|bool|array|object|null $msg = '', string $type = '', array $header = []): never redirect(string $url, int $code = 302, array $with = [], bool $safe = true): never
success()/error():HTML 请求渲染模板;Ajax / JSON 请求返回包含code、msg、data、url、wait的 JSON。error()未指定$url时,HTML 模板通过history.back()返回上一页。result():返回code、msg、time、data;$type支持json/jsonp/xml,为空或非法时回退 JSON。redirect():重定向并可通过 Session flash 传递数据;仅对不可信地址保留$safe = true。$url为null时优先读取同源 Referer,再回退default_url;传''表示不跳转。
目录结构
src/
├── Jump.php JumpManager.php Service.php # 入口类
├── jump/ 响应服务、配置快照、URL 策略与枚举
├── clock/ 可替换的时钟抽象与默认实现
├── contract/ JumpInterface 契约
├── facade/ Jump 门面
├── config/ 默认配置
├── console/ jump:publish 命令
└── tpl/ 内置跳转模板
开发
composer install composer analyse # PHPStan Level 9 composer test # Pest composer check # validate + analyse + test
许可证
木兰宽松许可证,第 2 版(MulanPSL-2.0),全文见 LICENSE。