目錄
- 地址表達式和參數(shù)
- 使用模塊/控制器/操作生成
- 使用控制器的方法生成
- 使用類的方法生成
- 直接使用路由地址
- URL后綴
- 域名生成
- 生成錨點
- 隱藏或者加上入口文件
本文實例講述了tp5.1 框架路由操作-URL生成。分享給大家供大家參考,具體如下:
ThinkPHP支持路由URL地址的統(tǒng)一生成,并且支持所有的路由方式,以及完美解決了路由地址的反轉(zhuǎn)解析,無需再為路由定義和變化而改變URL生成。
如果你開啟了路由延遲解析,需要生成路由映射緩存才能支持全部的路由地址的反轉(zhuǎn)解析。
URL生成使用 \think\facade\Url::build()
方法或者使用系統(tǒng)提供的助手函數(shù)url()
,參數(shù)一致:
Url::build('地址表達式',['參數(shù)'],['URL后綴'],['域名'])
url('地址表達式',['參數(shù)'],['URL后綴'],['域名'])
地址表達式和參數(shù)
對使用不同的路由地址方式,地址表達式的定義有所區(qū)別。參數(shù)單獨通過第二個參數(shù)傳入,假設我們定義了一個路由規(guī)則如下:
Route::rule('blog/:id','index/blog/read');
就可以使用下面的方式來生成URL地址:
Url::build('index/blog/read', 'id=5name=thinkphp');
Url::build('index/blog/read', ['id' => 5, 'name' => 'thinkphp']);
url('index/blog/read', 'id=5name=thinkphp');
url('index/blog/read', ['id' => 5, 'name' => 'thinkphp']);
使用模塊/控制器/操作生成
如果你的路由方式是路由到模塊/控制器/操作,那么可以直接寫
// 生成index模塊 blog控制器的read操作 URL訪問地址
Url::build('index/blog/read', 'id=5name=thinkphp');
// 使用助手函數(shù)
url('index/blog/read', 'id=5name=thinkphp');
以上方法都會生成下面的URL地址:
/index.php/blog/5/name/thinkphp.html
注意,生成方法的第一個參數(shù)必須和路由定義的路由地址保持一致,如果寫成下面的方式可能無法正確生成URL地址:
Url::build('blog/read','id=5name=thinkphp');
如果你的環(huán)境支持REWRITE,那么生成的URL地址會變?yōu)椋?/p>
/blog/5/name/thinkphp.html
如果你配置了:
那么生成的URL地址變?yōu)椋?/p>
/index.php/blog/5.html?name=thinkphp
不在路由規(guī)則里面的變量會直接使用普通URL參數(shù)的方式。
需要注意的是,URL地址生成不會檢測路由的有效性,只是按照給定的路由地址和參數(shù)生成符合條件的路由規(guī)則。
使用控制器的方法生成
如果你的路由地址是采用控制器的方法,并且路由定義如下:
// 這里采用配置方式定義路由 動態(tài)注冊的方式一樣有效
Route::get('blog/:id', '@index/blog/read');
那么可以使用如下方式生成:
// 生成index模塊 blog控制器的read操作 URL訪問地址
Url::build('@index/blog/read', 'id=5');
// 使用助手函數(shù)
url('@index/blog/read', 'id=5');
那么自動生成的URL地址變?yōu)椋?/p>
使用類的方法生成
如果你的路由地址是路由到類的方法,并且做了如下路由規(guī)則定義:
// 這里采用配置方式定義路由 動態(tài)注冊的方式一樣有效
Route::rule(['blog','blog/:id'],'\app\index\controller\blog@read');
如果路由地址是到類的方法,需要首先給路由定義命名標識,然后使用標識快速生成URL地址。
那么可以使用如下方式生成:
// 生成index模塊 blog控制器的read操作 URL訪問地址
Url::build('blog?id=5');
url('blog?id=5');
那么自動生成的URL地址變?yōu)椋?/p>
直接使用路由地址
我們也可以直接使用路由地址來生成URL,例如:
我們定義了路由規(guī)則如下:
Route::get('blog/:id' , 'index/blog/read');
可以使用下面的方式直接使用路由規(guī)則生成URL地址:
那么自動生成的URL地址變?yōu)椋?/p>
URL后綴
默認情況下,系統(tǒng)會自動讀取url_html_suffix
配置參數(shù)作為URL后綴(默認為html),如果我們設置了:
'url_html_suffix' => 'shtml'
那么自動生成的URL地址變?yōu)椋?/p>
如果我們設置了多個URL后綴支持
'url_html_suffix' => 'html|shtml'
則會取第一個后綴來生成URL地址,所以自動生成的URL地址還是:
如果你希望指定URL后綴生成,則可以使用:
Url::build('index/blog/read', 'id=5', 'shtml');
url('index/blog/read', 'id=5', 'shtml');
域名生成
默認生成的URL地址是不帶域名的,如果你采用了多域名部署或者希望生成帶有域名的URL地址的話,就需要傳入第四個參數(shù),該參數(shù)有兩種用法:
自動生成域名
Url::build('index/blog/read', 'id=5', 'shtml', true);
url('index/blog/read', 'id=5', 'shtml', true);
第四個參數(shù)傳入true
的話,表示自動生成域名,如果你開啟了url_domain_deploy
還會自動識別匹配當前URL規(guī)則的域名。
例如,我們注冊了域名路由信息如下:
Route::domain('blog','index/blog');
那么上面的URL地址生成為:
http://blog.thinkphp.cn/read/id/5.shtml
指定域名
你也可以顯式傳入需要生成地址的域名,例如:
Url::build('index/blog/read','id=5','shtml','blog');
url('index/blog/read','id=5','shtml','blog');
或者傳入完整的域名
Url::build('index/blog/read','id=5','shtml','blog.thinkphp.cn');
url('index/blog/read','id=5','shtml','blog.thinkphp.cn');
生成的URL地址為:
http://blog.thinkphp.cn/read/id/5.shtml
也可以直接在第一個參數(shù)里面?zhèn)魅胗蛎?,例如?/p>
Url::build('index/blog/read@blog', 'id=5');
url('index/blog/read@blog', 'id=5');
url('index/blog/read@blog.thinkphp.cn', 'id=5');
生成錨點
支持生成URL的錨點,可以直接在URL地址參數(shù)中使用:
Url::build('index/blog/read#anchor@blog','id=5');
url('index/blog/read#anchor@blog','id=5');
錨點和域名一起使用的時候,注意錨點在前面,域名在后面。
生成的URL地址為:
http://blog.thinkphp.cn/read/id/5.html#anchor
隱藏或者加上入口文件
有時候我們生成的URL地址可能需要加上index.php
或者去掉index.php
,大多數(shù)時候系統(tǒng)會自動判斷,如果發(fā)現(xiàn)自動生成的地址有問題,可以直接在調(diào)用build
方法之前調(diào)用root
方法,例如加上index.php
:
Url::root('/index.php');
Url::build('index/blog/read','id=5');
或者隱藏index.php
:
Url::root('/');
Url::build('index/blog/read','id=5');
root
方法只需要調(diào)用一次即可。
更多關于thinkPHP相關內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結》、《ThinkPHP常用方法總結》、《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術總結》。
希望本文所述對大家基于ThinkPHP框架的PHP程序設計有所幫助。
您可能感興趣的文章:- PHP tp5中使用原生sql查詢代碼實例
- tp5.1 框架數(shù)據(jù)庫-數(shù)據(jù)集操作實例分析
- tp5.1 框架join方法用法實例分析
- tp5.1框架數(shù)據(jù)庫子查詢操作實例分析
- tp5.1 框架數(shù)據(jù)庫常見操作詳解【添加、刪除、更新、查詢】
- TP5多入口設置實例講解