# 第4章 TP5.0 路由
==================================================
## 1、路由作用:
1、簡化URL地址,方便大家記憶
2、有利于搜索引擎優化
## 2、入口文件:
1、前后臺分離
a、在網站public目錄下(C:\AppServ\www\tp5\public) 新建admin.php
b、打開admin.php
<?php
// 定義應用目錄
define('APP_PATH', __DIR__ . '/../application/');
// 加載框架引導文件
require __DIR__ . '/../thinkphp/start.php';
2、綁定模塊
1、實現功能
index.php 這個入口文件 只能去前臺模塊
admin.php 這個入口文件 只能去后臺模塊 #建議后臺的入口文件稍微復雜一些
2、如何實現
在入口文件中
define("BIND_MODULE",'index'); # 綁定前臺模塊
define("BIND_MODULE",'admin'); # 綁定后臺模塊
3、URL地址發生改變
1、入口綁定之前
http://www.tp.com/admin.php/模塊/控制器/方法
2、入口綁定之后
http://www.tp.com/admin.php/控制器/方法
3、隱藏入口文件
1、開啟apache的重寫(C:\AppServ\Apache24\conf\httpd.conf)
# 把注釋開啟
LoadModule rewrite_module modules/mod_rewrite.so
2、設置訪問權限 (C:\AppServ\Apache24\conf\extra\httpd-vhosts.conf)
<VirtualHost *:80>
DocumentRoot "C:\AppServ\www\tp5\public"
ServerName www.tp5.com
<Directory "C:\AppServ\www\tp5\public">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
3、入口文件,在網站public目錄下新建.htaccess 文件
<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>
4、重啟服務
5、url地址變化
1、隱藏之前
http://www.tp.com/index.php/Index/test
2、隱藏之后
http://www.tp.com/Index/test
## 3、Tp5.0路由學習注意:
1、支持三種方式的URL解析規則
2、路由只針對應用,不針對模塊,因此路由的設置也是針對應用下面的所有模塊。
3、關閉后臺模塊,在后臺入口文件(C:\AppServ\www\tp5\public)
// 定義應用目錄
define('APP_PATH', __DIR__ . '/../application/');
// 綁定后臺
define('BIND_MODULE','admin');
// 加載框架引導文件
require __DIR__ . '/../thinkphp/start.php';
// 關閉admin模塊的路由
// 必須寫到 加載框架引導文件 之后否則報錯
\think\App::route(false);
## 4、路由模式
1、普通模式
a、定義
關閉路由,完全使用默認的 PATH_INFO 方式URL:
b、形式
http://www.tp.com/admin.php/index/index
c、如何設置
// 是否開啟路由
'url_route_on' => false,
// 是否強制使用路由
'url_route_must' => false,
2、混合模式
a、定義:
開啟路由,并使用路由定義+默認 PATH_INFO 方式的混合
b、如何設置
// 是否開啟路由
'url_route_on' => true,
// 是否強制使用路由
'url_route_must' => false,
3、強制模式
1、定義:
開啟路由,并設置必須定義路由才能訪問
2、如何設置
// 是否開啟路由
'url_route_on' => true,
// 是否強制使用路由
'url_route_must' => true,
## 5、設置路由-動態單個注冊
0、設置路由格式
Route::rule('路由表達式','路由地址','請求類型','路由參數(數組)','變量規則(數組)')
1、設置路由文件
C:\AppServ\www\tp5\application\route.php
2、如何設置
// 引入系統類
use think\Route;
// 定義路由規則
// 設置路由之后,就不能使用pathinfo訪問了
// 注冊路由 訪問到Index模塊index控制器index方法
Route::rule('/','index/index/index');
// 注冊路由test 訪問到Index模塊index控制器test方法
Route::rule('test','index/index/test');
3、路由的形式
1、靜態地址路由
// 注冊路由test 訪問到Index模塊index控制器test方法
Route::rule('test','index/index/test');
2、路由帶參數
// 注冊帶參數路由
// http://www.tp.com/couser/1
// http://www.tp.com/index/index/index/id/1
Route::rule('course/:id','index/index/course');
// 如果路由設置兩個參數,不許帶兩個參數
Route::rule('time/:year/:month','index/index/shijian');
3、可選參數路由
// http://www.tp.com/time/2017
// http://www.tp.com/time/2017/8
Route::rule('time/:year/[:month]','index/index/shijian');
4、全動態路由(不建議大家使用)
Route::rule(':a/:b','index/index/dongtai');
5、完全匹配路由
// http://www.tp.com/test1 #可以成功訪問
// http://www.tp.com/test1/1 #不能訪問
Route::rule('test1$','Index/index/test1');
6、路由額外帶參數
Route::rule('test2','Index/index/test2?id=10&name=zhangsan');
4、設置請求類型
1、TP中請求類型
get、post、put、delete
2、Route::rule() 默認支持所有請求類型
3、設置各種請求
// 支持get請求
Route::rule('type','Index/index/type','get');
// Route::get('type','Index/index/type');
// 支持post請求
// Route::rule('type','Index/index/type','post');
// Route::post('type','Index/index/type');
// 同時支持get和post
// Route::rule('type','Index/index/type','get|post');
// 支持所有路由
// Route::rule('type','Index/index/type','*');
// Route::any('type','Index/index/type');
// 支持put請求
Route::rule('type','Index/index/type','put');
Route::put('type','Index/index/type');
// 支持delete請求
Route::rule('type','Index/index/type','delete');
Route::delete('type','Index/index/type');
4、如何模擬put和delete請求
<form action="type" method="post">**
<p>
<input type="hidden" name="_method" value="PUT">**
<input type="text" name="name" id="">
</p>
<p>
<input type="submit" value="提交">
</p>
</form>
## 6、設置路由-動態批量注冊
1、基本格式
Route::rule([
'路由規則1'=>'路由地址和參數',
'路由規則2'=>['路由地址和參數','匹配參數(數組)','變量規則(數組)']
],'','請求類型','匹配參數(數組)','變量規則');
2、使用
Route::rule([
"test"=>"index/index/test",
"course/:id"=>"index/index/course"
],'','get');
Route::get([
"test"=>"index/index/test",
"course/:id"=>"index/index/course"
]);
## 7、設置路由-配置文件批量注冊
return [
"test"=>"index/index/test",
"course/:id"=>"index/index/course"
];
## 8、變量規則
// Route::rule('路由表達式','路由地址','請求類型','路由參數(數組)','變量規則(數組)');
// 設置路由參數id必須是數字,必須1-3位
Route::rule("course/:id","index/index/course",'get',[],['id'=>'\d{1,3}']);
## 9、路由參數
// Route::rule('路由表達式','路由地址','請求類型','路由參數(數組)','變量規則(數組)');
Route::rule("course/:id","index/index/course",'get',['method'=>'get','ext'=>'html'],['id'=>'\d{1,3}']);
// 路由參數method 請求方式必須是get
// 路由參數ext 主要設置路由的后綴
## 10、資源路由
1、聲明
Route::resource('blog','index/blog');
2、會自動注冊七個路由規則
get blog index # 后臺展示
get blog/create create # 添加頁面
post blog save # 增加操作
get blog/:id read
get blog/:id/edit edit # 修改頁面
put blog/:id update # 更新操作
delete blog/:id delete # 刪除操作
## 11、設置快捷路由
1、聲明
Route::Controller('blog','index/blog');
2、控制器中
namespace app\index\controller;
class Blog{
public function getindex(){
echo "我是bolg控制器index方法";
}
public function geta(){
echo "AAAAAAAA";
}
}
3、URL訪問
http://www.tp.com/blog/a
http://www.tp.com/blog/index
## 12、生成url地址
1、系統類
dump(Url::build('index/index/index'));
2、系統方法
dump(url('index/index/index'));
3、使用
// 普通url地址
dump(Url::build('index/index/index'));
dump(url('index/index/index'));
// 帶參數url
dump(url('index/index/abc',['id'=>10,'name'=>"張三"]));
dump(url('index/index/abc','id=10&name=100'));
// string(45) "/index/abc/id/10/name/%E5%BC%A0%E4%B8%89.html"
// string(30) "/index/abc/id/10/name/100.html"
// 帶錨點
dump(url('index/index/abc#name',['id'=>10,'name'=>"100"]));
// string(35) "/index/abc/id/10/name/100.html#name"
// 帶域名
dump(url('index/index/abc#name@blog',['id'=>10,'name'=>"100"]));
// string(53) "http://blog.tp.com/index/abc/id/10/name/100.html#name"
// 加入口文件
Url::root('/index.php');
dump(url('index/index/abc#name@blog',['id'=>10,'name'=>"100"]));
// string(63) "http://blog.tp.com/index.php/index/abc/id/10/name/100.html#name"