空中客車網(wǎng)站建設(shè)需求百度搜索下載app
ThinkPHP02:路由
- 一、路由定義
- 二、變量規(guī)則
- 三、路由地址
- 四、路由參數(shù)
- 五、路由分組
- 六、MISS
- 七、資源路由
- 八、注解路由
- 九、URL生成
一、路由定義
-
路由默認(rèn)開啟,在 config/app.php 中可以關(guān)閉路由。
-
路由配置在 config/route.php 中,路由定義在 route/app.php 中。
-
rule()
默認(rèn)請(qǐng)求是any。Route::rule("details/:id", "Address/details", "GET|POST");
-
其他的有 get、post等。
Route::get("details/:id", 'Address/details');
二、變量規(guī)則
-
系統(tǒng)默認(rèn)路由變量規(guī)則是 \w+,即字母、數(shù)字、中文和下劃線,在 config/route.php 中可更改。
-
在路由中設(shè)置變量規(guī)則。
Route::get("details/:id", 'Address/details')->pattern(["id" => '\d+']);
-
動(dòng)態(tài)組合拼裝,地址和參數(shù)都是動(dòng)態(tài)的。
Route::get("hello-<name>-<id>", 'Hello:name/details')->pattern(["id" => '\d+']);
-
閉包路由可以通過URL直接執(zhí)行,而不需要控制器和方法,也支持傳遞參數(shù)和動(dòng)態(tài)規(guī)則。
Route::get('think/:name', function ($name) {return 'hello, '.$name.'!'; });
三、路由地址
-
路由地址一般為:控制器/操作方法。
-
多級(jí)控制器路由。
Route::rule('blog/:id', 'group.Blog/details');
-
完整路徑
Route::rule('ds/:id', "\app\controller\Address@details");
-
路由重定向
Route::redirect("ds/:id", "http://localhost:8000", 302);
四、路由參數(shù)
-
設(shè)置路由時(shí),可以設(shè)置相關(guān)的方法,實(shí)施匹配檢測(cè)和行為執(zhí)行。
-
ext()
方法用于檢測(cè)URL后綴、強(qiáng)制所有URL后綴,denyExt()
禁止使用后綴。Route::get("details/:id", 'Address/details')->ext('html|shtml');
-
https()
方法用于檢測(cè)是否為 https 協(xié)議。Route::get("details/:id", 'Address/details')->https();
-
domain()
方法檢測(cè)當(dāng)前域名是否匹配,完整域名和子域名均可。Route::get("details/:id", 'Address/details')->domain("localhost");
-
ajax/pjax/json
用于檢測(cè)當(dāng)前的頁面是否是以上請(qǐng)求方式。Route::get("details/:id", 'Address/details')->ajax();
-
filter()
用于檢測(cè)額外參數(shù),額外參數(shù)可以表單提交。Route::get("details/:id", 'Address/details')->filter(['id' => 5, "type" => 1]);
-
append()
方法用于追加額外參數(shù)。Route::get("details/:id", 'Address/details')->append(['status' => 1]);
-
allowCrossDomain()
方法可以解決跨域請(qǐng)求。oute::get("details/:id", 'Address/details')->allowCrossDomain(["Access-Control-Allow-Origin" => "*" ]);
-
option()
用于配置多個(gè)參數(shù)Route::get("details/:id", 'Address/details')->option(['ext' => 'html','https' => true ]);
五、路由分組
-
路由分組可以將相同前綴的路由合并分組,簡化路由定義。
Route::group("address", function () {Route::rule("ds/:id", "/details");Route::rule("rd/:id", "/read"); })->prefix('Address');
-
路由規(guī)則定義的文件,加載時(shí)會(huì)解析消耗較多資源,可以在 config/route.php 中開啟延遲解析,在匹配的時(shí)候才會(huì)注冊(cè)解析。
'url_lazy_route' => true,
六、MISS
-
MISS在匹配不到相應(yīng)規(guī)則時(shí)會(huì)自動(dòng)跳轉(zhuǎn)到 MISS。
-
全局MISS。
# app/controller/Error.php public function miss() {return "404 Not Found"; }# route/app.php Route::miss('public/miss');
-
分組MISS
# app/controller/Address.php public function miss() {return '404 Address'; }# route/app.php Route::group("address", function () {Route::rule("ds/:id", "/details");Route::rule("rd/:id", "/read");Route::miss("miss"); })->prefix('Address');
七、資源路由
-
系統(tǒng)提供了快速生成資源控制器的命令。
php think make:controller Blog
-
注冊(cè)資源路由,注冊(cè)成功后,會(huì)自動(dòng)提供CURD方法,無需手動(dòng)注冊(cè),請(qǐng)求方式有GET、POST、PUT、DELETE。
Route::resource('blog', 'Blog');# 自動(dòng)注冊(cè) http://localhost:8000/blog/ (index) http://localhost:8000/blog/5 (read) http://localhost:8000/blog/5/edit (edit)
-
默認(rèn)參數(shù)采用id名稱。也可以自定義
# route/app.php Route::resource('blog', 'Blog')->vars(['blog' => 'blog_id']);# app/controller/Blog.php public function read($blog_id) {return "顯示指定的資源: ". $blog_id; }
-
only()
用于限定資源方法,except()
用于排除系統(tǒng)提供的資源方法。Route::resource('blog', 'Blog')->only(['index', 'read']); Route::resource('blog', 'Blog')->except(['delete', 'update']);
-
rest()
更改系統(tǒng)給予的默認(rèn)方法,放在resource方法前面。資源路由的標(biāo)識(shí)不可更改,但生成的路由規(guī)則和對(duì)應(yīng)操作方法可以修改。# rest方法要放在resource前面 Route::rest([# 資源路由標(biāo)識(shí) => 請(qǐng)求方式,請(qǐng)求地址,操作方法'save' => ["POST", "/:id/save", "save"],'update' => ["PUT", "/:id", "update"], ]); Route::resource('blog', 'Blog');
-
使用資源嵌套路由,可以讓上級(jí)資源對(duì)下級(jí)資源進(jìn)行操作。
# app/controller/Comment.php class Comment {public function read($id, $blog_id) {return "評(píng)論ID:" . $id . ",博客ID:" . $blog_id;}public function edit($id, $blog_id) {return "評(píng)論ID:" . $id . ",博客ID:" . $blog_id;} }# route/app.php 注冊(cè)資源嵌套路由 Route::resource("blog.comment", "Comment");# 路由規(guī)則 http://localhost:8000/blog/:blog_id/comment/:id
八、注解路由
-
注解路由是在注解中寫的路由,項(xiàng)目很簡單時(shí)使用。
-
路由注解方式,并非系統(tǒng)默認(rèn)支持,而是可選方案,需要額外安裝擴(kuò)展。
composer require topthink/think-annotation
-
在控制器中寫路由,必須使用雙引號(hào)。單引號(hào)不能解析
# app/controller/Address.php use think\annotation\Route;class Address {/*** @param $id* @return string* @route("ds/:id", method="GET", https=1);*/public function details($id) {return '詳情id:' . $id;} }
-
注解模式也支持資源路由和分組。
use think\annotation\Route\Resource;/*** Class Blog* @package app\controller* @Resource("blog");*/ class Blog {... }
九、URL生成
-
使用
buildUrl()
獲取路由的URL地址。默認(rèn)后綴是html,可以使用suffix()
更改。使用domain()
可以生成加上域名的URL。Route::buildUrl("Url/details", ['id' => 8])->suffix('shtml')->domain(true);
-
可以給路由定義一個(gè)別名,生成URL時(shí)使用別名調(diào)用。
-
可以直接使用路由地址生成URL。
-
助手函數(shù)
url()
可以直接代替Route::buildUrl()
url(ds/5)->domain(true);