1、在config文件夹的app.php中添加配置模板文件的路径 以及默认输出类型 6版本阉割掉了这几个配置需要手动添加
image
代码如下
// 默认输出类型
'default_return_type' => 'html',
// 默认AJAX 数据返回格式,可选json xml ...
'default_ajax_return' => 'json',
// 默认JSONP格式返回的处理方法
'default_jsonp_handler' => 'jsonpReturn',
// 默认JSONP处理方法
'var_jsonp_handler' => 'callback',
// 默认跳转页面对应的模板文件
'dispatch_success_tmpl' => app()->getThinkPath() . 'tpl/dispatch_jump.tpl',
'dispatch_error_tmpl' => app()->getThinkPath() . 'tpl/dispatch_jump.tpl',
2、添加dispatch_jump.tpl文件到think_exception.tpl同级目录(vendor\topthink\framework\src\tpl)下 可以去tp5去拷贝
image
3、在基类BaseController中添加下面的代码
image
image
image
一定要注意 复制下面的代码 不要去复制5版本的jump类文件 因为你要改很多东西 注意图片中带红线的 tp6读取配置信息 需要添加上级 否则无法读取
use think\Container;
use think\exception\HttpResponseException;
use think\Response;
use think\response\Redirect;
/**
* 操作成功跳转的快捷方法
* @access protected
* @param mixed $msg 提示信息
* @param string $url 跳转的URL地址
* @param mixed $data 返回的数据
* @param integer $wait 跳转等待时间
* @param array $header 发送的Header信息
* @return void
*/
protected function success($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
{
if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
$url = $_SERVER["HTTP_REFERER"];
} elseif ('' !== $url) {
$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : Container::get('url')->build($url);
}
$result = [
'code' => 1,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
$type = $this->getResponseType();
// 把跳转模板的渲染下沉,这样在 response_send 行为里通过getData()获得的数据是一致性的格式
if ('html' == strtolower($type)) {
$response = view($this->app->config->get('app.dispatch_success_tmpl'), $result);
} else {
if ($type == 'json') {
$response = json($result);
}
}
throw new HttpResponseException($response);
}
/**
* 操作错误跳转的快捷方法
* @access protected
* @param mixed $msg 提示信息
* @param string $url 跳转的URL地址
* @param mixed $data 返回的数据
* @param integer $wait 跳转等待时间
* @param array $header 发送的Header信息
* @return void
*/
protected function error($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
{
if (is_null($url)) {
$url = $this->app['request']->isAjax() ? '' : 'javascript:history.back(-1);';
} elseif ('' !== $url) {
$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : $this->app['url']->build($url);
}
$result = [
'code' => 0,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
$type = $this->getResponseType();
if ('html' == strtolower($type)) {
$response = view($this->app->config->get('app.dispatch_error_tmpl'), $result);
} else {
if ($type == 'json') {
$response = json($result);
}
}
throw new HttpResponseException($response);
}
/**
* 返回封装后的API数据到客户端
* @access protected
* @param mixed $data 要返回的数据
* @param integer $code 返回的code
* @param mixed $msg 提示信息
* @param string $type 返回数据格式
* @param array $header 发送的Header信息
* @return void
*/
protected function result($data, $code = 0, $msg = '', $type = '', array $header = [])
{
$result = [
'code' => $code,
'msg' => $msg,
'time' => time(),
'data' => $data,
];
$type = $type ?: $this->getResponseType();
$response = Response::create($result, $type)->header($header);
throw new HttpResponseException($response);
}
/**
* @param $url 重定向地址
* @param $data 重定向传参 ['name' => 'think']
* @return Redirect
*/
protected function redirect($url, $data)
{
return redirect((string)url($url, $data));
}
/**
* 获取当前的response 输出类型
* @access protected
* @return string
*/
protected function getResponseType()
{
if (!$this->app) {
$this->app = Container::get('app');
}
$isAjax = $this->app['request']->isAjax();
$config = $this->app['config'];
return $isAjax ? $config->get('app.default_ajax_return') : $config->get('app.default_return_type');
}
到此步骤调用
return $this->success('操作成功', '/admin/user/create', ['from' => 'index']);
时 会报错
thinkphp6 Driver [Think] not supported.
这是因为缺少模板引擎
在thinkphp6.0手册中搜索模板引擎 并安装
composer require topthink/think-view
问题即可解决
具体用法跟tp5相同 不同的是rediect方法是我自己看tp6手册简单封装的 有些功能没有需要使用者自行封装
谢谢大家啦