您好,登錄后才能下訂單哦!
如何進(jìn)行TP5重定向,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
學(xué)習(xí)目標(biāo)
如果要使用頁(yè)面跳轉(zhuǎn)必須要繼承基類Controller
類,因?yàn)榛?code>Controller引入了trait
類庫(kù),trait
類庫(kù)又實(shí)現(xiàn)了success()
和error()
的跳轉(zhuǎn)方法。
1、當(dāng)前控制器
來(lái)到默認(rèn)模塊默認(rèn)控制器中演示,首先繼承自基類Controller
,在當(dāng)前Index
控制器中創(chuàng)建一個(gè)hello()
方法來(lái)模擬網(wǎng)站的后臺(tái)登陸和頁(yè)面的跳轉(zhuǎn):
class Index extends \think\Controller { public function index() { return 'TP5學(xué)習(xí)ing....'; } public function hello($name) { if ($name=='thinkphp') { $this->success('驗(yàn)證成功,正在跳轉(zhuǎn)~~','ok'); } else { $this->error('驗(yàn)證失敗,返回登陸頁(yè)面','login'); } } public function ok() { return '歡迎使用本系統(tǒng)'; } public function login() { return '登陸頁(yè)面'; } }
根據(jù)邏輯,如果訪問的url
是:http://tp5.com/index/index/hello/name/thinkphp
,會(huì)執(zhí)行success()
方法,跳轉(zhuǎn)到ok()
方法,反之,使用:http://tp5.com/index/index/hello/name/tp5
,則會(huì)走error()
方法,跳轉(zhuǎn)到login()
方法。
2、跨控制器
首先創(chuàng)建一個(gè)新的控制器Login
,把Index
中的ok()
和login()
方法剪切到文件中:
<?php namespace app\index\controller; class Login extends \think\Controller { public function ok() { return '歡迎使用本系統(tǒng)'; } public function login() { return '登陸頁(yè)面'; } }
然后,Index
控制器也進(jìn)行修改:
<?php namespace app\index\controller; class Index extends \think\Controller { public function index() { return 'TP5學(xué)習(xí)ing....'; } public function hello($name) { if ($name=='thinkphp') { $this->success('驗(yàn)證成功,正在跳轉(zhuǎn)~~','login/ok'); } else { $this->error('驗(yàn)證失敗,返回登陸頁(yè)面','login/login'); } } }
這樣就實(shí)現(xiàn)了跨控制器的跳轉(zhuǎn),我們來(lái)驗(yàn)證一下:http://tp5.com/index/index/hello/name/thinkphp
和http://tp5.com/index/index/hello/name/tp5
都能正常跳轉(zhuǎn)。
3、跨模塊
首先創(chuàng)建一個(gè)demo
模塊,模塊下創(chuàng)建控制器Login.php
,把上個(gè)例子的Login.php
的內(nèi)容拷貝過(guò)去,修改下命名空間,保存。
然后修改下Index
控制器:
public function hello($name) { if ($name=='thinkphp') { $this->success('驗(yàn)證成功,正在跳轉(zhuǎn)~~','demo/login/ok'); } else { $this->error('驗(yàn)證失敗,返回登陸頁(yè)面','demo/login/login'); } }
這樣就實(shí)現(xiàn)了跨模塊的跳轉(zhuǎn),我們來(lái)驗(yàn)證一下:http://tp5.com/index/index/hello/name/thinkphp
和http://tp5.com/index/index/hello/name/tp5
依舊都能正常跳轉(zhuǎn)。
4、外部地址
如果跳轉(zhuǎn)為外部地址的話,記得前提是必須要以協(xié)議開頭!
演示也很簡(jiǎn)單,修改一下error()
方法跳轉(zhuǎn)的地址:
$this->error('驗(yàn)證失敗,返回登陸頁(yè)面','http://www.baidu.com');
使用http://tp5.com/index/index/hello/name/tp5
測(cè)試一下,成功跳轉(zhuǎn)到百度。
補(bǔ)充說(shuō)明
補(bǔ)充一下,這個(gè)跳轉(zhuǎn)地址,除了可以用字符串的形式外,還可以路由的方式來(lái)生成。示例如下:
$this->success('驗(yàn)證成功,正在跳轉(zhuǎn)~~',\think\Url::build('demo/login/ok'));
如果覺得這樣寫比較繁瑣,還可以通過(guò)注入函數(shù)進(jìn)行簡(jiǎn)化:
$this->success('驗(yàn)證成功,正在跳轉(zhuǎn)~~',url('demo/login/ok'));
也沒有問題。
使用redirect(路由地址,請(qǐng)求變量,后綴,是否顯示域名)
方法
演示一下,因?yàn)檠菔臼翘D(zhuǎn)到同一控制器中的,只需要前兩個(gè)參數(shù):
class Index extends \think\Controller { public function hello($name) { if ($name=='thinkphp') { $this->redirect('ok',['siteName'=>'百度']); } } public function ok($siteName) { return '成功就會(huì)跳轉(zhuǎn)到 '.$siteName.' 網(wǎng)址。'; } }
我們來(lái)驗(yàn)證一下:http://tp5.com/index/index/hello/name/thinkphp
,可以正常跳轉(zhuǎn)。
同樣的,重定向也支持外部地址:
public function hello($name) { if ($name=='thinkphp') { $this->redirect('ok',['siteName'=>'百度']); } else { $this->redirect('http://www.baidu.com',302);//302是臨時(shí)重定向,301是永久重定向 } }
驗(yàn)證一下:http://tp5.com/index/index/hello/name/tp5
,直接重定向到百度了。
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。