午夜91福利视频,午夜成人在线观看,午夜在线视频免费观看,午夜福利短视频,精品午夜成人免费视频APP

URL生成

ThinkPHP5.0支(zhi)持路(lu)由URL地址(zhi)的統一生(sheng)成,并(bing)且支(zhi)持所(suo)有的路(lu)由方(fang)式(shi),以(yi)及完美(mei)解決了(le)路(lu)由地址(zhi)的反轉解析,無(wu)需再為路(lu)由定義和(he)變化而改(gai)變URL生(sheng)成。

版本 更新功能
5.0.10 增加app_host配置參數用于設置當前生成URL的根地址
  增加https_agent_name配置參數用于設置當前CDN或者代理的https標識名

URL生成使用 \think\Url::build() 方法或者使用系統提供的助手函數url(),參數一致:

Url::build('地址表達式',['參數'],['URL后綴'],['域名'])

url('地址表達式',['參數'],['URL后綴'],['域名'])

地址表達式和參數

對使用不同的(de)路由(you)地(di)址方式(shi)(shi),地(di)址表(biao)達式(shi)(shi)的(de)定(ding)義(yi)有所區(qu)別。參數單獨通過第二個參數傳入(ru),假設我(wo)們定(ding)義(yi)了一個路由(you)規(gui)則如(ru)下:

Route::rule('blog/:id','index/blog/read');

就可以使用(yong)下面(mian)的(de)方式來生成URL地址:

Url::build('index/blog/read','id=5&name=thinkphp');
Url::build('index/blog/read',['id'=>5,'name'=>'thinkphp']);
url('index/blog/read','id=5&name=thinkphp');
url('index/blog/read',['id'=>5,'name'=>'thinkphp']);

下面我們統一使用第一種方式講解。

使用模塊/控制器/操作生成

如果你的路由方式是路由到模塊/控(kong)制(zhi)器/操作,那么(me)可以直接(jie)寫(xie)

// 生成index模(mo)塊(kuai) blog控制器(qi)的(de)read操作 URL訪(fang)問(wen)地址
Url::build('index/blog/read','id=5&name=thinkphp');
// 使用助手函數
url('index/blog/read','id=5&name=thinkphp');

以上(shang)方(fang)法都會(hui)生(sheng)成下面的URL地址:

/index.php/blog/5/name/thinkphp.html

注意(yi),生成方法的第一(yi)個(ge)參數必須和(he)路由定義的路由地址保持一(yi)致(zhi),如果寫成下面的方式可能無法正確生成URL地址:

Url::build('blog/read','id=5&name=thinkphp');

如果(guo)你的環(huan)境支持REWRITE,那么生成的URL地(di)址會(hui)變為:

/blog/5/name/thinkphp.html

如(ru)果你配置了:

'url_common_param'=>true

那(nei)么生(sheng)成(cheng)的(de)URL地(di)址變為(wei):

/index.php/blog/5.html?name=thinkphp

不(bu)在(zai)路由規則里面的(de)變量(liang)會(hui)直接使(shi)用普通URL參數的(de)方式。

需(xu)要注意的是(shi)(shi),URL地址(zhi)生成不會檢(jian)測路(lu)由(you)的有效性(xing),只是(shi)(shi)按照(zhao)給定的路(lu)由(you)地址(zhi)和參數生成符合(he)條件的路(lu)由(you)規則。

使用控制器的方法生成

如果你的路(lu)由地址是(shi)采用(yong)控制器的方法,并且路(lu)由定義如下:

// 這里采(cai)用配置方式定(ding)義路由(you) 動(dong)態注冊的方式一樣(yang)有效(xiao)
'blog/:id'  => '@index/blog/read'

那么可以使(shi)用如下方式生成:

// 生成(cheng)index模塊 blog控制器(qi)的read操(cao)作 URL訪問(wen)地(di)址(zhi)
Url::build('@index/blog/read','id=5');
// 使用助手函(han)數(shu)
url('@index/blog/read','id=5');

那么自(zi)動生成的URL地址變為:

/index.php/blog/5.html

使用類的方法生成

如果你的路(lu)由地址是路(lu)由到類(lei)的方法(fa),并且(qie)做(zuo)了如下路(lu)由規(gui)則定義:

// 這里采用配置方(fang)式(shi)定(ding)義路由 動態注冊的方(fang)式(shi)一樣有效
Route::rule(['blog','blog/:id'],'\app\index\controller\blog@read');

如(ru)果(guo)路由(you)地(di)(di)址是到(dao)類的方法(fa),需要首先給(gei)路由(you)定義命(ming)名標識(shi)(shi),然后使用標識(shi)(shi)快速(su)生成URL地(di)(di)址。

那么可以使用(yong)如下方式生成:

// 生(sheng)成index模(mo)塊 blog控制器(qi)的read操作(zuo) URL訪(fang)問(wen)地址
Url::build('blog?id=5');
url('blog?id=5');

那么(me)自動生(sheng)成的URL地址變為(wei):

/index.php/blog/5.html

直接使用路由地址

我們也可以直(zhi)接(jie)使(shi)用路由地址來生成URL,例如:

我們定義了路由規則如下:

'blog/:id' => 'index/blog/read'

可以使(shi)用(yong)下面的方(fang)式直接使(shi)用(yong)路由規則生成URL地(di)址(zhi):

Url::build('/blog/5');

那(nei)么自(zi)動生成的URL地址變為:

/index.php/blog/5.html

URL后綴

默認情況下,系統會自動讀取url_html_suffix配置參數(shu)作為URL后綴(zhui)(默(mo)認為html),如果我(wo)們設置了:

'url_html_suffix'   => 'shtml'

那么自動生成的URL地址變為:

/index.php/blog/5.shtml

如(ru)果我們設置了多個URL后綴(zhui)支(zhi)持

'url_html_suffix'   => 'html|shtml'

則會取第一(yi)個后綴來(lai)生成URL地(di)址(zhi),所以自(zi)動生成的URL地(di)址(zhi)還是:

/index.php/blog/5.html

如果你希望指定(ding)URL后綴(zhui)生成,則可以(yi)使(shi)用:

Url::build('index/blog/read','id=5','shtml');
url('index/blog/read','id=5','shtml');

域名生成

默認生(sheng)成(cheng)的URL地址(zhi)(zhi)是不帶域名的,如(ru)果你采用(yong)了多域名部(bu)署或者希望生(sheng)成(cheng)帶有(you)域名的URL地址(zhi)(zhi)的話(hua),就需要(yao)傳入第四(si)個參數(shu),該參數(shu)有(you)兩種用(yong)法:

自動生成域名

Url::build('index/blog/read','id=5','shtml',true);
url('index/blog/read','id=5','shtml',true);

第四個參數傳入true的話,表示自動生成域名,如果你開啟了url_domain_deploy還會自動識別匹配當前URL規則的域名(ming)。

例如,我們注冊了域名(ming)路由信息如下:

Route::domain('blog','index/blog');

那么上面(mian)的URL地址生成為(wei):

http://blog.thinkphp.cn/read/id/5.shtml

指定域名

你也可(ke)以顯式(shi)傳入需要生成地址的域(yu)名(ming),例如:

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

也可以直接在第一個參數里(li)面(mian)傳入域名,例如:

Url::build('index/blog/read@blog','id=5');
url('index/blog/read@blog','id=5');
url('index/blog/read@blog.thinkphp.cn','id=5');

生成錨點

支(zhi)持生成URL的錨點,可以直接(jie)在(zai)URL地址參(can)數中使用:

Url::build('index/blog/read#anchor@blog','id=5');
url('index/blog/read#anchor@blog','id=5');

錨點和域名一起(qi)使用(yong)的時候,注(zhu)意錨點在(zai)前面,域名在(zai)后面。

生成(cheng)的URL地址為:

http://blog.thinkphp.cn/read/id/5.html#anchor

隱藏或者加上入口文件

有時候我們生成的URL地址可能需要加上index.php或者去掉index.php,大多數時候系統會自動判斷,如果發現自動生成的地址有問題,可以直接在調用build方法之前調用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方法只需要(yao)調用一(yi)次即可。

文檔最后更新時間:2018-04-25 19:37:14

文檔
目錄

深色
模式

切換
寬度(du)