路由的作用:
1. 簡化URL地址,方便大家記憶
2. 有利於搜索引擎的優化,比如可以被百度的爬蟲抓取到
優化URl
1. 前后端分離
修改入口文件,在public下新建admin.php文件,將下面的代碼添加進入
1 // 定義應用目錄 2 3 define('APP_PATH', __DIR__ . '/../application/'); 4 5 // 加載框架引導文件 6 7 require __DIR__ . '/../thinkphp/start.php';
2.綁定模塊
1)前后端分離實現的功能
index.php 這個入口文件只能進入前台模塊
admin.php 這個入口文件只能進入后台模塊
2)綁定模塊
在index.php添加 define(‘BIND_MODULE’,’index’); 這樣http://www.demo.com/index.php/只能訪問前台模塊。訪問不了后台,http://www.yd.com/index.php/index/index
在admin.php添加 define(‘BIND_MODULE’,’admin’); 這樣http://www.demo.com/admin.php只能訪問后台模塊,訪問不了前台,http://www.yd.com/admin.php/index/index
3) 隱藏入口文件(怎么操作就不寫了,可以看下文檔里面的URL訪問下 的隱藏入口文件 的說明),這樣訪問前台模塊可以省去index.php,可以用http://www.yd.com/index/index直接訪問到
關閉后台的路由
在public下的admin.php中添加這句代碼 \think\App::route(false);
// 定義應用目錄 define('APP_PATH', __DIR__ . '/../application/'); //綁定后台 define('BIND_MODULE','admin'); // 加載框架引導文件 require __DIR__ . '/../thinkphp/start.php'; //關閉admin模塊的路由,必須寫到加載框架引導文件之后 \think\App::route(false);
路由的三種模式:
1. 普通模式 :完全使用PASH_INFO來訪問,比如http://www.yd.com/index.php/index/index,域名+模塊+控制器
2. 混合模式 :可以使用路由也可以不使用
3. 強制模式 :必須使用路由
設置路由
一.動態單個設置
在application下的route.php文件內更改
1 use think\Route; //引入Route 2 3 Route::rule('test','index/index/demo'); //當URL訪問http://www.yd.com/test時,訪問的是index模塊下的index下的控制器下的demo方法
路由形式:
靜態路由:Route::rule(‘test’,’index/index/demo’);
帶參數的路由: Route::rule(‘getid/:id’,’index/User/getId’);
比如我訪問http://www.yd.com/getid/7,或者http://www.yd.com/getid/8,或者http://www.yd.com/getid/9,就是getid后面帶個參數
1 //首先在index模塊下的User控制器中寫一個getId方法 2 3 public function getId(){ 4 5 echo input('id'); //輸出id 6 7 } 8 9 //然后在route.php加上這行代碼 10 11 Route::rule('getid/:id','index/User/getId'); 12 13 //最后當我們http://www.yd.com/getid后面加個數字,比如http://www.yd.com/getid/20,頁面會顯示20
帶多個參數路由,比如帶兩個參數
1 //index模塊下的User控制器中寫一個myTime方法 2 3 public function myTime(){ 4 5 echo input('year').' 年 '.input('month').'月'; //輸出 n年n月 6 7 } 8 9 //然后在route.php加上這行代碼 10 11 Route::rule('time/:year/:month','index/User/myTime'); 12 13 //最后當我們訪問http://www.yd.com/time/2018/9,頁面會顯示2018 年 9月
選擇性帶參數,就是我們在訪問url時,URL后面可以帶參數,也可以不帶,在寫路由文件上的參數帶上中括號就行
比如輸出年或年月
1 public function myTime(){ 2 3 echo input('year').' 年 '.input('month').'月'; //輸出 n年n月 4 5 } 6 7 //然后在route.php加上這行代碼 8 9 Route::rule('time/:year/[:month]','index/User/myTime'); //重點:month外面加[] 10 11 //最后當我們訪問http://www.yd.com/time/2018/9,頁面會顯示2018 年 9月 12 13 //當我們訪問http://www.yd.com/time/2018,頁面會顯示2018 年 月
純帶參數的路由 不建議使用
1 //路由寫法 2 3 Route::rule(':x/:y','index/User/XAndY'); 4 5 //方法 6 7 public function XAndY(){ 8 9 echo input('x').' '.input('y'); 10 11 } 12 13 //訪問http://www.yd.com/5/3,頁面輸出5 3
完全匹配路由 在路由的后面加個$符號
1 public function comp(){ 2 3 echo '我是完全匹配路由'; 4 5 } 6 7 //不加$符號,我們字comp后面加多少路徑,比如http://www.yd.com/comp/asdfda/asdfasfd/aaa/bbb,頁面都能輸出 我是完全匹配路由 8 9 Route::rule('comp','index/User/comp'); 10 11 12 13 //加上$符號,我們在comp后面加多少路徑,比如http://www.yd.com/comp/asdfda/asdfasfd/aaa/bbb,頁面不能輸出方法的內容 14 15 Route::rule('comp','index/User/comp');
二.批量設置路由
第一種寫法,將上面所有單個動態注冊的路由批量注冊
1 Route::rule([ 2 3 "test"=>"index/index/demo", 4 5 'getid/:id'=>'index/User/getId', 6 7 'time/:year/[:month]'=>'index/User/myTime', 8 9 ':x/:y'=>'index/User/XAndY', 10 11 'comp$'=>'index/User/comp' 12 13 ],'','get');
第二種方式,這里用get舉例
1 Route::get([ 2 3 "test"=>"index/index/demo", 4 5 'getid/:id'=>'index/User/getId', 6 7 'time/:year/[:month]'=>'index/User/myTime', 8 9 ':x/:y'=>'index/User/XAndY', 10 11 'comp$'=>'index/User/comp' 12 13 ]);
3.配置文件設置路由,使用配置文件批量注冊,還是在route.php文件內寫
1 return[ 2 3 "test"=>"index/index/demo", 4 5 'getid/:id'=>'index/User/getId', 6 7 'time/:year/[:month]'=>'index/User/myTime', 8 9 ':x/:y'=>'index/User/XAndY', 10 11 'comp$'=>'index/User/comp' 12 13 ];
路由的請求方式
TP里面有四種請求方式,GET,POST,PUT,DELETE四種方式,如果我們不指定請求類型,默認是*,所有的請求類型
請求方式有兩種寫法,這里用get舉例
1 Route::rule('qtype','index/User/questType','get'); 2 3 Route::get('gtype','index/User/questType');
既支持get有支持post的寫法
Route::rule('type','index/User/questType','get|post');
全部請求方式都支持的兩種寫法
Route::any('type','index/User/questType');
Route::rule('type','index/User/questType','*');
變量規則,Route::rule();的最后一個參數,是一個數組,可以指定多個參數,用正則表達式來寫,用來規范傳入的參數必須是什么數據類型,或者必須是那些數據等等,比如
Route::rule('getid/:id','index/User/getId','get',[],['id'=>'\d']); //最后一個參數,表示id傳參數必須是數字
路由參數,Route::rule();的倒數第二個參數,是一個數組,可以用來指定請求的數據類型,也可以用來規定請求的URL后綴,比如
Route::rule('getid/:id','index/User/getId','get',['method'=>'get','ext'=>'html'],['id'=>'\d']); //請求方式必須是get,請求的后綴必須是html,訪問的url為http://www.yd.com/getid/9.html,不帶html后綴就請求失敗
資源路由,你的后台模塊可能會有增刪改查等操作,但是一個一個寫太費勁,資源路由自動幫你生這些路由,你只需要在控制器內寫這些方法,
比如
1 //先創建block 2 3 namespace app\index\controller; 4 5 class Block 6 7 { 8 9 public function index(){ 10 11 echo '我是前台模塊下的block'; 12 13 } 14 15 public function create(){ 16 17 echo '我是前台模塊下的block的create方法'; 18 19 } 20 21 public function read($id){ 22 23 echo $id; 24 25 } 26 27 } 28 29 //然后在route.php下寫上資源路由 30 31 Route::resource('block','index/Block'); 32 33 34 35 //效果: 36 37 //當你訪問http://www.yd.com/block URL訪問的是index方法 38 39 //當你訪問http://www.yd.com/block/15 URL訪問的是read方法 40 41 //當你訪問http://www.yd.com/block/create URL訪問的是create方法
快捷路由
在index模塊下創建一個Fastroute控制器,里面寫下如下例子,除了index,其他方法都要加上get
1 namespace app\index\controller; 2 3 class Fastroute 4 5 { 6 7 public function index(){ 8 9 echo '我是Fast路由的index'; 10 11 } 12 13 public function getAA(){ 14 15 echo "我是getAA"; 16 17 } 18 19 public function getBB(){ 20 21 echo "我是BB"; 22 23 } 24 25 public function postInfo() 26 27 { 28 29 } 30 31 32 33 public function putInfo() 34 35 { 36 37 } 38 39 40 41 public function deleteInfo() 42 43 { 44 45 } 46 47 }
在route.php里面寫下快捷路由
1 //注意:路由名字要和控制器名字一樣 2 3 Route::controller('Fastroute','index/Fastroute'); 4 5 //然后我們想訪問getAA方法,我們可以通過訪問URL http://www.yd.com/Fastroute/AA來訪問 6 7 //想訪問getBB(),可以通過 http://www.yd.com/Fastroute/BB來訪問 8 1
生成URL 兩種方式,不太懂有什么用
1 Url::build(‘index/User/index’); 2 Url::build(); 3 4 5 Url::root('/index.php'); //帶入口文件 6 7 dump(Url('index/User/index')); 8 9 dump(Url::build('index/User/index'));