Home  >  Article  >  PHP Framework  >  thinkphp6 redirect tips

thinkphp6 redirect tips

爆肝混世大魔王-流笙 第五人格服务
爆肝混世大魔王-流笙 第五人格服务Original
2021-05-04 18:51:21186browse

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