首页 php框架 ThinkPHP thinkphp6重定向提示

thinkphp6重定向提示

May 04, 2021 pm 06:51 PM
1

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中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1666
14
CakePHP 教程
1426
52
Laravel 教程
1328
25
PHP教程
1273
29
C# 教程
1254
24