路由的做用:
1. 簡化URL地址,方便你們記憶
2. 有利於搜索引擎的優化,好比能夠被百度的爬蟲抓取到php
優化URl
1. 先後端分離
修改入口文件,在public下新建admin.php文件,將下面的代碼添加進入html
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);thinkphp
// 定義應用目錄 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. 強制模式 :必須使用路由後端
設置路由數組
一.動態單個設置app
在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
帶多個參數路由,好比帶兩個參數post
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'));