thinkphp6 リダイレクトのヒント

爆肝混世大魔王-流笙 第五人格服务
リリース: 2021-05-06 09:22:45
オリジナル
37 人が閲覧しました

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' =>getRootPath( ) . '/public/ tpl/dispatch_jump.tpl',
————————————————

場所は /app/BaseController にあります。 .php

(1) 以下の 2 行のコードを先頭に追加すると、インポートされたファイルは次のようになります:

//

// 次の 2 行は、古いバージョンの成功エラー リダイレクトにジャンプするために使用されます

//
use thinkExceptionHttpResponseException;
use thinkfacadeRequest;
———— ————————————


2) 次に、この基本コントローラ クラス ファイルに変更を加え、次のコードを直接追加します。

//
// 以下は新発です、旧バージョンを使用するため成功エラーリダイレクトを跳转 start
//

/**
* ジャンプ成功のショートカット方法
* @access protected
* @parammixed $msg プロンプトメッセージ
* @param string $url ジャンプURLアドレス
* @parammixed $data 返されるデータ
* @param integer $wait ジャンプ待ち時間
* @param array $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,
'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
* @parammixed $msg プロンプトメッセージ
* @param string $url ジャンプURLアドレス
* @parammixed $data 返されるデータ
* @param integer $wait ジャンプ待ち時間
* @param array $header ヘッダー情報を送信します
* @return void
*/
保護された関数エラー($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,
'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 配列 $暗黙的なパラメータ渡し
* @return void
*/
protected function redirect($url, $params = [], $code = 302, $with = [])
{
$response = Response::create($url, 'リダイレクト');

if (is_integer($params)) {
$code = $params;
$params = [];
}

$response->code($code)->params($ params)->with($with);

throw new HttpResponseException($response);
}

/**
* 現在の応答出力タイプを取得します
* @access protected
* @return string
*/
保護関数 getResponseType()
{
return $this->request- >isJson() || $this->request->isAjax() ? 'json' : 'html';
}

//
//以上新增,旧バージョン使用のため成功エラーリダイレクト跳转 end
//

リダイレクトプロンプトを使用して3秒後にコードプロンプトにジャンプする場合は次のとおりです

//検証失敗の処理
->success($p, url('/admin/index/Add_order_list/')) ; // リダイレクトとプロンプト情報

以上がthinkphp6 リダイレクトのヒントの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
1
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!