1. 程式人生 > >ThinkPHP5之路由

ThinkPHP5之路由

路由的作用:
1. 簡化URL地址,方便大家記憶
2. 有利於搜尋引擎的優化,比如可以被百度的爬蟲抓取到

優化URl
1. 前後端分離
修改入口檔案,在public下新建admin.php檔案,將下面的程式碼新增進入

// 定義應用目錄
define('APP_PATH', __DIR__ . '/../application/');
// 載入框架引導檔案
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檔案內更改

use think\Route;                        //引入Route           
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後面帶個引數

    //首先在index模組下的User控制器中寫一個getId方法
    public function  getId(){
        echo input('id');           //輸出id
    }
    //然後在route.php加上這行程式碼
    Route::rule('getid/:id','index/User/getId');
    //最後當我們http://www.yd.com/getid後面加個數字,比如http://www.yd.com/getid/20,頁面會顯示20

帶多個引數路由,比如帶兩個引數

    //index模組下的User控制器中寫一個myTime方法
    public function myTime(){
        echo input('year').' 年 '.input('month').'月';            //輸出 n年n月
    }
     //然後在route.php加上這行程式碼
     Route::rule('time/:year/:month','index/User/myTime');
     //最後當我們訪問http://www.yd.com/time/2018/9,頁面會顯示2018 年 9月

選擇性帶引數,就是我們在訪問url時,URL後面可以帶引數,也可以不帶,在寫路由檔案上的引數帶上中括號就行
比如輸出年或年月

 public function myTime(){
        echo input('year').' 年 '.input('month').'月';            //輸出 n年n月
    }
     //然後在route.php加上這行程式碼
     Route::rule('time/:year/[:month]','index/User/myTime');    //重點:month外面加[]
     //最後當我們訪問http://www.yd.com/time/2018/9,頁面會顯示2018 年 9月
     //當我們訪問http://www.yd.com/time/2018,頁面會顯示2018 年 月

純帶引數的路由 不建議使用

//路由寫法
Route::rule(':x/:y','index/User/XAndY');
//方法
public function XAndY(){
        echo input('x').'  '.input('y');
    }
//訪問http://www.yd.com/5/3,頁面輸出5 3

完全匹配路由 在路由的後面價格$符號

public function comp(){
        echo '我是完全匹配路由';
}
//不加$符號,我們字comp後面加多少路徑,比如http://www.yd.com/comp/asdfda/asdfasfd/aaa/bbb,頁面都能輸出     我是完全匹配路由
Route::rule('comp','index/User/comp');

//加上$符號,我們在comp後面加多少路徑,比如http://www.yd.com/comp/asdfda/asdfasfd/aaa/bbb,頁面不能輸出方法的內容
Route::rule('comp','index/User/comp');

二.批量設定路由
第一種寫法,將上面所有單個動態註冊的路由批量註冊

Route::rule([
    "test"=>"index/index/demo",
    'getid/:id'=>'index/User/getId',
    'time/:year/[:month]'=>'index/User/myTime',
    ':x/:y'=>'index/User/XAndY',
    'comp$'=>'index/User/comp'
],'','get');

第二種方式,這裡用get舉例

Route::get([
    "test"=>"index/index/demo",
    'getid/:id'=>'index/User/getId',
    'time/:year/[:month]'=>'index/User/myTime',
    ':x/:y'=>'index/User/XAndY',
    'comp$'=>'index/User/comp'
]);

3.配置檔案設定路由,使用配置檔案批量註冊,還是在route.php檔案內寫

return[
    "test"=>"index/index/demo",
    'getid/:id'=>'index/User/getId',
    'time/:year/[:month]'=>'index/User/myTime',
    ':x/:y'=>'index/User/XAndY',
    'comp$'=>'index/User/comp'
];

路由的請求方式
TP裡面有四種請求方式,GET,POST,PUT,DELETE四種方式,如果我們不指定請求型別,預設是*,所有的請求型別

請求方式有兩種寫法,這裡用get舉例

Route::rule('qtype','index/User/questType','get');
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字尾就請求失敗

資源路由,你的後臺模組可能會有增刪改查等操作,但是一個一個寫太費勁,資源路由自動幫你生這些路由,你只需要在控制器內寫這些方法,

這裡寫圖片描述

比如

//先建立block
namespace app\index\controller;
class Block
{
    public function index(){
        echo '我是前臺模組下的block';
    }
    public function create(){
        echo '我是前臺模組下的block的create方法';
    }
    public function read($id){
        echo $id;
    }
}
//然後在route.php下寫上資源路由
Route::resource('block','index/Block');

//效果:
//當你訪問http://www.yd.com/block         URL訪問的是index方法
//當你訪問http://www.yd.com/block/15      URL訪問的是read方法
//當你訪問http://www.yd.com/block/create   URL訪問的是create方法

快捷路由
在index模組下建立一個Fastroute控制器,裡面寫下如下例子,除了index,其他方法都要加上get

namespace app\index\controller;
class Fastroute
{
   public function index(){
        echo '我是Fast路由的index';
    }
    public function getAA(){
        echo "我是getAA";
    }
    public function getBB(){
        echo "我是BB";
    }
    public function postInfo()
    {
    }

    public function putInfo()
    {
    }

    public function deleteInfo()
    {
    }
}

在route.php裡面寫下快捷路由

//注意:路由名字要和控制器名字一樣
Route::controller('Fastroute','index/Fastroute');
//然後我們想訪問getAA方法,我們可以通過訪問URL   http://www.yd.com/Fastroute/AA來訪問
//想訪問getBB(),可以通過   http://www.yd.com/Fastroute/BB來訪問

生成URL 兩種方式,不太懂有什麼用
Url::build(‘index/User/index’);
Url::build();

        Url::root('/index.php');            //帶入口檔案
        dump(Url('index/User/index'));
        dump(Url::build('index/User/index'));