首页 > php框架 > ThinkPHP > 正文

thinkphp6重定向提示

爆肝混世大魔王-流笙 第五人格服务
发布: 2021-05-06 09:22:45
原创
38 人浏览过

1、准备跳转模板文件

把旧的 ThinkPHP 框架的中的跳转模板文件复制过来

位置在 旧的框架核心文件 /thinkphp/tpl/dispatch_jump.tpl

比如放到:/public/tpl/dispatch_jump.tpl

2、修改配置文件

位置在 /config/app.php 把如下2行代码加在配置文件中,如下:

// 默认跳转页面对应的模板文件【新增】
‘dispatch_success_tmpl’ => app()->getRootPath() . ‘/public/tpl/dispatch_jump.tpl’,
‘dispatch_error_tmpl’ => app()->getRootPath() . ‘/public/tpl/dispatch_jump.tpl’,
————————————————

3、修改基础控制器类

位置在 /app/BaseController.php

(1)在顶部加上如下2行代码,引入的文件,如下:

//
// 下面2行,为了使用旧版的 success error redirect 跳转
//
use think\exception\HttpResponseException;
use think\facade\Request;
————————————————

2)接下来,还是在这个基础控制器类文件里进行修改,直接加入如下代码:“

//
// 以下为新增,为了使用旧版的 success error redirect 跳转  start
//

/**
* 操作成功跳转的快捷方法
* @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 = '', string $url = null, $data = '', int $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 : app('route')->buildUrl($url);
   }

   $result = [
       'code' => 1,
       'msg'  => $msg,
       'data' => $data,
       'url'  => $url,
       'wait' => $wait,
   ];

   $type = $this->getResponseType();
   if ($type == 'html'){
       $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 = '', string $url = null, $data = '', int $wait = 3, array $header = [])
{
   if (is_null($url)) {
       $url = $this->request->isAjax() ? '' : 'javascript:history.back(-1);';
   } elseif ($url) {
       $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : $this->app->route->buildUrl($url);
   }

   $result = [
       'code' => 0,
       'msg'  => $msg,
       'data' => $data,
       'url'  => $url,
       'wait' => $wait,
   ];

   $type = $this->getResponseType();
   if ($type == 'html'){
       $response = view($this->app->config->get('app.dispatch_error_tmpl'), $result);
   } else if ($type == 'json') {
       $response = json($result);
   }
   throw new HttpResponseException($response);
}

/**
* URL重定向  自带重定向无效
* @access protected
* @param  string         $url 跳转的URL表达式
* @param  array|integer  $params 其它URL参数
* @param  integer        $code http code
* @param  array          $with 隐式传参
* @return void
*/
protected function redirect($url, $params = [], $code = 302, $with = [])
{
   $response = Response::create($url, 'redirect');

   if (is_integer($params)) {
       $code   = $params;
       $params = [];
   }

   $response->code($code)->params($params)->with($with);

   throw new HttpResponseException($response);
}

/**
* 获取当前的response 输出类型
* @access protected
* @return string
*/
protected function getResponseType()
{
   return $this->request->isJson() || $this->request->isAjax() ? 'json' : 'html';
}

//
// 以上为新增,为了使用旧版的 success error redirect 跳转  end
//

使用重定向提示3秒后跳转代码提示案例如下

//验证失败做处理
           $p = $v->getError(); //提示的信息是验证器里的错误信息
           $this->success($p, url('/admin/index/Add_order_list/')); //重定向并提示信息

以上是thinkphp6重定向提示的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
1
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!