<track id="vsnnr"><ruby id="vsnnr"><menu id="vsnnr"></menu></ruby></track>

  1. 更多>>PHP程序設計 Blog

    關于 ThinkPHP6 無法使用 success(), error() 跳轉的解決方法

    對于在 ThinkPHP3,ThinkPHP5 中習慣了使用 success(), error() 跳轉的小伙伴,本文將介紹如何在 ThinkPHP6 中使用這些跳轉。

    在 ThinkPHP6 中使用 諸如 $this->error('驗證碼錯誤'); 的提示中,會發現不能用了,原來是官方把 success,error 這些跳轉給取消了。


    在ThinkPHP官方發布的 ThinkPHP6完全開發手冊 - 附錄 - 升級指導 中,給出了如下說明:

    “系統不再提供基礎控制器類think\Controller,原來的success、error、redirect和result方法需要自己在基礎控制器類里面實現?!?/p>

    “系統默認在應用目錄下面提供了一個app\BaseController基礎類,或者你可以直接放入你的應用里面,繼承使用?!?/p>


    下面本文將給出如何在 ThinkPHP6 中繼續使用 $this->success(),$this->error() 這些跳轉。


    1、準備跳轉模板文件

    把舊的 ThinkPHP 框架的中的跳轉模板文件復制過來

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

    比如放到:/public/tpl/dispatch_jump.tpl 這個位置,如圖所示:

    124040534781510.jpg


    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',

    注意文件的路徑,和第 1 步中保持一致,加好后如圖所示:

    124048592432258.jpg


    3、修改基礎控制器類

    位置在 /app/BaseController.php


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

    //
    // 下面2行,為了使用舊版的 success error redirect 跳轉
    //
    use think\exception\HttpResponseException;
    use think\facade\Request;

    加好后如圖所示:

    124059659767509.jpg


    (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
    	//


    4、測試跳轉效果

    像舊版框架一樣,比如使用:

    $this->error('請輸入驗證碼');

    結果如圖所示:

    124140894337735.jpg

    達到了預期的效果。


    評論列表

    no_photo
    great info 2023   來自烏克蘭
    Thanks for excellent info I was looking for this info for my mission.

    2023-01-01 04:08:23

    no_photo
    By Van Bryan, Editorial Direct   來自烏克蘭
    One of America’s most accurate tech investors says this tech could trigger https://artevinostudio.com/myphp/buyneurontinonline/ the single biggest financial event since 1602

    2022-08-20 04:00:31

    no_photo
    fantastic information   來自烏克蘭
    I am not sure where you’re getting your information, but good topic. I needs to spend some time learning much more or understanding more. Thanks for great information I was looking for this info for my mission.

    2022-08-08 01:29:36

    no_photo
    Test, just a test   來自香港特別行政區
    Njfhsjdwkdjwfh jiwkdwidwhidjwi jiwkdowfiehgejikdoswfiw https://gehddijiwfugwdjaidheufeduhwdwhduhdwudw.com/fjhdjwksdehfjhejdsdefhe

    2022-07-13 16:58:09

    no_photo
    The sci-fi technology tackling   來自烏克蘭
    The sci-fi technology tackling malarial mosquitos - Environmental campaigner Liz O'Neill doesn't mince her words about gene drives - the next generation of genetic modification (GM) technology https://neurontinline.tumblr.com/

    2022-05-25 21:51:49

    發表評論

    用來接收審核回復提醒,請認真填寫

      換一張?
    captcha
    看不清?點擊圖片換一張
    黄色大片中文字欧美特片网_亚洲欧美国产图片视频_日韩黄色精品国产成人毛片_国产综合亚洲欧洲区
      <track id="vsnnr"><ruby id="vsnnr"><menu id="vsnnr"></menu></ruby></track>