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 학습자의 빠른 성장을 도와주세요!