search
HomePHP FrameworkThinkPHPthinkphp6 redirect tips

1. Prepare the jump template file

Copy the jump template file from the old ThinkPHP framework

The location is in the old framework core file/thinkphp/tpl/dispatch_jump. tpl

For example, place it in: /public/tpl/dispatch_jump.tpl

2. Modify the configuration file

in /config/app.php and add the following 2 lines of code In the configuration file, it is as follows:

// Template file corresponding to the default jump page [New]
'dispatch_success_tmpl' => app()->getRootPath() . '/public/ tpl/dispatch_jump.tpl',
'dispatch_error_tmpl' => app()->getRootPath() . '/public/tpl/dispatch_jump.tpl',
—————————— ——————

3. Modify the base controller class

location in /app/BaseController.php

(1) Add the following at the top 2 lines of code, the imported file is as follows:

//
// The following 2 lines, in order to use the old version of success error redirect jump
//
use think\exception\HttpResponseException ;
use think\facade\Request;
————————————————

2) Next, still in this basic controller Make modifications in the class file and directly add the following code: "

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

/**
* Quick method for successful jump
* @access protected
* @param mixed $msg Prompt message
* @param string $url Jump URL address
* @param mixed $data Returned data
* @param integer $wait Jump waiting time
* @param array $header Header information sent
* @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);
}

/**
* Quick method for error jump
* @access protected
* @param mixed $msg prompt message
* @param string $url Jump URL address
* @param mixed $data Returned data
* @param integer $wait Jump waiting time
* @param array $header Header information sent
* @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 redirection The built-in redirection is invalid
* @access protected
* @param string $url Jump URL expression
* @param array|integer $params Other URL parameters
* @param integer $code http code
* @param array $with implicit parameter passing
* @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);
}

/**
* Get the current response output type
* @access protected
* @return string
*/
protected function getResponseType()
{
   return $this->request->isJson() || $this->request->isAjax() ? 'json' : 'html';
}

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

Use the redirect prompt to jump code after 3 seconds. Code reminder cases are as follows

// Verification failure is processed. Error message in the server
         $this->success($p, url('/admin/index/Add_order_list/')); //Redirect and prompt information

The above is the detailed content of thinkphp6 redirect tips. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor