前面的話
本文將詳細介紹thinkphp5URL和路由
URL訪問
ThinkPHP采用單一入口模式訪問應用,對應用的所有請求都定向到應用的入口文件,系統會從URL參數中解析當前請求的模塊、控制器和操作,下面是一個標準的URL訪問格式:
http://domainName/index.php/模塊/控制器/操作
其中index.php就稱之為應用的入口文件(注意入口文件可以被隱藏,后面會提到)
模塊在ThinkPHP中的概念其實就是應用目錄下面的子目錄,而官方的規范是目錄名小寫,因此模塊全部采用小寫命名,無論URL是否開啟大小寫轉換,模塊名都會強制小寫
應用的index模塊的Index控制器定義如下:
?php
namespace app\index\controller;
class Index
{
public function index()
{
return 'index';
}
public function hello($name = 'World')
{
return 'Hello,' . $name . '!';
}
}
如果直接訪問入口文件的話,由于URL中沒有模塊、控制器和操作,因此系統會訪問默認模塊(index)下面的默認控制器(Index)的默認操作(index),因此下面的訪問是等效的:
http://tp5.com/index.php
http://tp5.com/index.php/index/index/index
如果要訪問控制器的hello方法,則需要使用完整的URL地址
http://tp5.com/index.php/index/index/hello/name/thinkphp
訪問URL地址后頁面輸出結果為:
Hello,thinkphp!
由于name參數為可選參數,因此也可以使用
http://tp5.com/index.php/index/index/hello
訪問URL地址后頁面輸出結果為:
Hello,World!
默認情況下,URL地址中的控制器和操作名是不區分大小寫的,因此下面的訪問其實是等效的:
http://tp5.com/index.php/index/Index/Index
http://tp5.com/index.php/index/INDEX/INDEX
如果控制器是駝峰的,例如定義一個HelloWorld控制器(application/index/controller/HelloWorld.php):
?php
namespace app\index\controller;
class HelloWorld
{
public function index($name = 'World')
{
return 'Hello,' . $name . '!';
}
}
正確的URL訪問地址(該地址可以使用url方法生成)應該是
http://tp5.com/index.php/index/hello_world/index
系統會自動定位到HelloWorld控制器類去操作
如果使用
http://tp5.com/index.php/index/HelloWorld/index
將會報錯,并提示Helloworld控制器類不存在
如果希望嚴格區分大小寫訪問(這樣就可以支持駝峰法進行控制器訪問),可以在應用配置文件中設置:
// 關閉URL自動轉換(支持駝峰訪問控制器)
'url_convert' => false,
關閉URL自動轉換之后,必須使用下面的URL地址訪問(控制器名稱必須嚴格使用控制器類的名稱,不包含控制器后綴):
http://tp5.com/index.php/index/Index/index
http://tp5.com/index.php/index/HelloWorld/index
如果服務器環境不支持pathinfo方式的URL訪問,可以使用兼容方式,例如:
http://tp5.com/index.php?s=/index/Index/index
其中變量s的名稱的可以配置的
5.0不再支持普通的URL訪問方式,所以下面的訪問是無效的,你會發現無論輸入什么,訪問的都是默認的控制器和操作
http://tp5.com/index.php?m=indexc=Indexa=hello
參數傳入
通過操作方法的參數綁定功能,可以實現自動獲取URL的參數,仍然以上面的控制器為例,控制器代碼如下:
?php
namespace app\index\controller;
class Index
{
public function index()
{
return 'index';
}
public function hello($name = 'World')
{
return 'Hello,' . $name . '!';
}
}
當我們訪問
http://tp5.com/index.php/index/index/hello
就是訪問app\index\controller\Index控制器類的hello方法,因為沒有傳入任何參數,name參數就使用默認值World。如果傳入name參數,則使用:
http://tp5.com/index.php/index/index/hello/name/thinkphp
頁面輸出結果為:
Hello,thinkphp!
現在給hello方法增加第二個參數:
public function hello($name = 'World', $city = '')
{
return 'Hello,' . $name . '! You come from ' . $city . '.';
}
訪問地址為http://tp5.com/index.php/index/index/hello/name/thinkphp/city/shanghai
頁面輸出結果為:
Hello,thinkphp! You come from shanghai.
可以看到,hello方法會自動獲取URL地址中的同名參數值作為方法的參數值,而且這個參數的傳入順序不受URL參數順序的影響,例如下面的URL地址輸出的結果和上面是一樣的:
http://tp5.com/index.php/index/index/hello/city/shanghai/name/thinkphp
或者使用http://tp5.com/index.php/index/index/hello?city=shanghainame=thinkphp
還可以進一步對URL地址做簡化,前提就是我們必須明確參數的順序代表的變量,我們更改下URL參數的獲取方式,把應用配置文件中的url_param_type參數的值修改如下:
// 按照參數順序獲取
'url_param_type' => 1,
現在,URL的參數傳值方式就變成了嚴格按照操作方法的變量定義順序來傳值了,也就是說我們必須使用下面的URL地址訪問才能正確傳入name和city參數到hello方法:http://tp5.com/index.php/index/index/hello/thinkphp/shanghai
頁面輸出結果為:
Hello,thinkphp! You come from shanghai.
如果改變參數順序為http://tp5.com/index.php/index/index/hello/shanghai/thinkphp
頁面輸出結果為:
Hello,shanghai! You come from thinkphp.
顯然不是我們預期的結果。
同樣,我們試圖通過http://tp5.com/index.php/index/index/hello/name/thinkphp/city/shanghai
訪問也不會得到正確的結果
[注意]按順序綁定參數的話,操作方法的參數只能使用URL pathinfo變量,而不能使用get或者post變量
隱藏入口
可以去掉URL地址里面的入口文件index.php,但是需要額外配置WEB服務器的重寫規則。
以Apache為例,需要在入口文件的同級添加.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>
如果用的phpstudy,規則如下:
IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1]
/IfModule>
接下來就可以使用下面的URL地址訪問了
http://tp5.com/index/index/index
http://tp5.com/index/index/hello
如果使用的apache版本使用上面的方式無法正常隱藏index.php,可以嘗試使用下面的方式配置.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>
如果是Nginx環境的話,可以在Nginx.conf中添加:
location / { // …..省略部分代碼
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
break;
}
}
定義路由
URL地址里面的index模塊怎么才能省略呢,默認的URL地址顯得有點長,下面就來說說如何通過路由簡化URL訪問。
我們在路由定義文件(application/route.php)里面添加一些路由規則,如下:
return [
// 添加路由規則 路由到 index控制器的hello操作方法
'hello/:name' => 'index/index/hello',
];
該路由規則表示所有hello開頭的并且帶參數的訪問都會路由到index控制器的hello操作方法。
路由之前的URL訪問地址為:http://tp5.com/index/index/hello/name/thinkphp
定義路由后就只能訪問下面的URL地址http://tp5.com/hello/thinkphp
[注意]定義路由規則后,原來的URL地址將會失效,變成非法請求。
但這里有一個小問題,如果我們只是訪問http://tp5.com/hello
將發生錯誤
事實上這是由于路由沒有正確匹配到,我們修改路由規則如下:
return [
// 路由參數name為可選
'hello/[:name]' => 'index/hello',
];
使用[]把路由規則中的變量包起來,就表示該變量為可選,接下來就可以正常訪問了http://tp5.com/hello
當name參數沒有傳入值的時候,hello方法的name參數有默認值World,所以輸出的內容為 Hello,World!
除了路由配置文件中定義之外,還可以采用動態定義路由規則的方式定義,例如在路由配置文件(application/route.php)的開頭直接添加下面的方法:
use think\Route;
Route::rule('hello/:name', 'index/hello');
完成的效果和使用配置方式定義是一樣的。
無論是配置方式還是通過Route類的方法定義路由,都統一放到路由配置文件application/route.php文件中
[注意]路由配置不支持在模塊配置文件中設置
【完整匹配】
前面定義的路由是只要以hello開頭就能進行匹配,如果需要完整匹配,可以使用下面的定義:
return [
// 路由參數name為可選
'hello/[:name]$' => 'index/hello',
];
當路由規則以$結尾的時候就表示當前路由規則需要完整匹配。
當我們訪問下面的URL地址的時候:
http://tp5.com/hello // 正確匹配
http://tp5.com/hello/thinkphp // 正確匹配
http://tp5.com/hello/thinkphp/val/value // 不會匹配
【閉包定義】
還支持通過定義閉包為某些特殊的場景定義路由規則,例如:
return [
// 定義閉包
'hello/[:name]' => function ($name) {
return 'Hello,' . $name . '!';
},
];
或者
use think\Route;
Route::rule('hello/:name', function ($name) {
return 'Hello,' . $name . '!';
});
[注意]閉包函數的參數就是路由規則中定義的變量
因此,當訪問下面的URL地址:http://tp5.com/hello/thinkphp
會輸出
Hello,thinkphp!
【設置URL分隔符】
如果需要改變URL地址中的pathinfo參數分隔符,只需要在應用配置文件(application/config.php)中設置:
// 設置pathinfo分隔符
'pathinfo_depr' => '-',
路由規則定義無需做任何改變,我們就可以訪問下面的地址:http://tp5.com/hello-thinkphp
【路由參數】
還可以約束路由規則的請求類型或者URL后綴之類的條件,例如:
return [
// 定義路由的請求類型和后綴
'hello/[:name]' => ['index/hello', ['method' => 'get', 'ext' => 'html']],
];
上面定義的路由規則限制了必須是get請求,而且后綴必須是html的,所以下面的訪問地址:
http://tp5.com/hello // 無效
http://tp5.com/hello.html // 有效
http://tp5.com/hello/thinkphp // 無效
http://tp5.com/hello/thinkphp.html // 有效
【變量規則】
接下來,嘗試一些復雜的路由規則定義滿足不同的路由變量。在此之前,首先增加一個控制器類如下:
?php
namespace app\index\controller;
class Blog
{
public function get($id)
{
return '查看id=' . $id . '的內容';
}
public function read($name)
{
return '查看name=' . $name . '的內容';
}
public function archive($year, $month)
{
return '查看' . $year . '/' . $month . '的歸檔內容';
}
}
添加如下路由規則:
return [
'blog/:year/:month' => ['blog/archive', ['method' => 'get'], ['year' => '\d{4}', 'month' => '\d{2}']],
'blog/:id' => ['blog/get', ['method' => 'get'], ['id' => '\d+']],
'blog/:name' => ['blog/read', ['method' => 'get'], ['name' => '\w+']],
];
在上面的路由規則中,我們對變量進行的規則約束,變量規則使用正則表達式進行定義。
我們看下幾種URL訪問的情況
// 訪問id為5的內容
http://tp5.com/blog/5
// 訪問name為thinkphp的內容
http://tp5.com/blog/thinkphp
// 訪問2015年5月的歸檔內容
http://tp5.com/blog/2015/05
【路由分組】
上面的三個路由規則由于都是blog打頭,所以我們可以做如下的簡化:
return [
'[blog]' => [
':year/:month' => ['blog/archive', ['method' => 'get'], ['year' => '\d{4}', 'month' => '\d{2}']],
':id' => ['blog/get', ['method' => 'get'], ['id' => '\d+']],
':name' => ['blog/read', ['method' => 'get'], ['name' => '\w+']],
],
];
對于這種定義方式,我們稱之為路由分組,路由分組一定程度上可以提高路由檢測的效率
【復雜路由】
有時候,還需要對URL做一些特殊的定制,例如如果要同時支持下面的訪問地址
http://tp5.com/blog/thinkphp
http://tp5.com/blog-2015-05
我們只要稍微改變路由定義規則即可:
return [
'blog/:id' => ['blog/get', ['method' => 'get'], ['id' => '\d+']],
'blog/:name' => ['blog/read', ['method' => 'get'], ['name' => '\w+']],
'blog-year>-month>' => ['blog/archive', ['method' => 'get'], ['year' => '\d{4}', 'month' => '\d{2}']],
];
對 blog-year>-month> 這樣的非正常規范,我們需要使用變量名>這樣的變量定義方式,而不是 :變量名方式。
簡單起見,我們還可以把變量規則統一定義,例如:
return [
// 全局變量規則定義
'__pattern__' => [
'name' => '\w+',
'id' => '\d+',
'year' => '\d{4}',
'month' => '\d{2}',
],
// 路由規則定義
'blog/:id' => 'blog/get',
'blog/:name' => 'blog/read',
'blog-year>-month>' => 'blog/archive',
];
在__pattern__中定義的變量規則我們稱之為全局變量規則,在路由規則里面定義的變量規則我們稱之為局部變量規則,如果一個變量同時定義了全局規則和局部規則的話,當前的局部規則會覆蓋全局規則的,例如:
return [
// 全局變量規則
'__pattern__' => [
'name' => '\w+',
'id' => '\d+',
'year' => '\d{4}',
'month' => '\d{2}',
],
'blog/:id' => 'blog/get',
// 定義了局部變量規則
'blog/:name' => ['blog/read', ['method' => 'get'], ['name' => '\w{5,}']],
'blog-year>-month>' => 'blog/archive',
];
URL生成
定義路由規則之后,可以通過Url類來方便的生成實際的URL地址(路由地址),針對上面的路由規則,我們可以用下面的方式生成URL地址。
// 輸出 blog/thinkphp
Url::build('blog/read', 'name=thinkphp');
Url::build('blog/read', ['name' => 'thinkphp']);
// 輸出 blog/5
Url::build('blog/get', 'id=5');
Url::build('blog/get', ['id' => 5]);
// 輸出 blog/2015/05
Url::build('blog/archive', 'year=2015month=05');
Url::build('blog/archive', ['year' => '2015', 'month' => '05']);
[注意]build方法的第一個參數使用路由定義中的完整路由地址
還可以使用系統提供的助手函數url來簡化
url('blog/read', 'name=thinkphp');
// 等效于
Url::build('blog/read', 'name=thinkphp');
通常在模板文件中輸出的話,可以使用助手函數,例如:
{:url('blog/read', 'name=thinkphp')}
如果我們的路由規則發生調整,生成的URL地址會自動變化
如果你配置了url_html_suffix參數的話,生成的URL地址會帶上后綴,例如:
'url_html_suffix' => 'html',
那么生成的URL地址 類似
blog/thinkphp.html
blog/2015/05.html
如果你的URL地址全部采用路由方式定義,也可以直接使用路由規則來定義URL生成,例如:
url('/blog/thinkphp');
Url::build('/blog/8');
Url::build('/blog/archive/2015/05');
生成方法的第一個參數一定要和路由定義的路由地址保持一致,如果你的路由地址比較特殊,例如使用閉包定義的話,則需要手動給路由指定標識,例如:
// 添加hello路由標識
Route::rule(['hello','hello/:name'], function($name){
return 'Hello,'.$name;
});
// 根據路由標識快速生成URL
Url::build('hello', 'name=thinkphp');
// 或者使用
Url::build('hello', ['name' => 'thinkphp']);
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- thinkPHP5.1框架路由::get、post請求簡單用法示例
- thinkphp5框架路由原理與用法詳解
- thinkPHP5框架路由常用知識點匯總
- Thinkphp5 如何隱藏入口文件index.php(URL重寫)
- thinkPHP5.0框架URL訪問方法詳解
- thinkphp四種url訪問方式詳解
- thinkphp的URL路由規則與配置實例
- thinkphp中的url跳轉用法分析
- tp5.1 框架路由操作-URL生成實例分析