一個獨立,易於使用,可擴展且簡單地了解PHP Web應用程序URL路由庫,Mybe增強您的開發工具包或為您提供了一些靈感。
下載源代碼並放置項目目錄的正確位置。
獲取路由器對象實例
use RouterOne Router ;
$ router = Router:: getInstance ();在單個文件中創建路由映射,並位於Projject的特定DIR中,在文件$this請參考混凝土$router Object實例。對於HTTP請求動詞, RouteOne僅支持GET , POST (只需與PHP的$ _GET&$ _POST保持一致,如果需要或必要,請完全擴展。
例如, Foo.php 。 Routerone可以支持多個路由映射文件,例如frontend_route_map.php , backend_route_map.php ,。
// Closure function call
$ this -> get ( ' / ' , function () {
echo ' Hello! RouterOne ' ;
});
// `WelcomeController::hello()` call
$ this -> get ( ' / ' , [WeclomeController::class, ' hello ' ]);
設置路由圖文件目錄路徑並加載它。 (路由圖文件默認擴展名為.php )
$ router -> setIncludePath (` YOUR_ROUTE_MAP_FILE_DIR `);
$ router -> load ([ ' Foo ' ]); // Just file's name without extension或這樣打電話
$ router -> setIncludePath (` YOUR_ROUTE_MAP_FILE_DIR `)-> load ([ ' Foo ' ]); // Just file's name without extension運行調度並啟用所有已加載的路線,將返回有關實際調用路由的結果。
$ res = $ router -> dispatch ();控制器路線行動:
// Http GET
$ this -> get ( ' index ' , [ Controllers SiteController::class, ' index ' ]);
// Http POST
$ this -> post ( ' news/add ' , [ Controllers NewsController::class, ' add ' ]);封閉路線行動:
// Http GET
$ this -> get ( ' index ' , function () {
/**
* Some logic process code here
*/
});
// Http POST
$ this -> post ( ' news/add ' , function () {
/**
* Some logic process code here
*/
});使用路由參數,包裝{}動態參數,然後通過外觀順序轉移到控制器方法或Clousre函數參數。
$ this -> get ( ' test/{param1}/{param2} ' , [ Controllers TestController::class, ' params ' ]);
class TestController
{
public function params ( $ param1 , $ param2 )
{
// Some code ...
}
} $ this -> get ( ' test/{param1}/{param2} ' , function ( $ param1 , $ param2 ) {
// Some code
});中間件應該是對RouteMiddleWareInterface的實現,您可以在任意目錄中找到中間軟件類文件,例如MiddleWare Dir;
一個典型的中間軟件類包含帶有路由Action- $action參數的句柄handle()方法,如下:
use RouterOne MiddleWare RouteMiddleWareInterface ;
class AuthCheckMiddleWare implements RouteMiddleWareInterface
{
public static function handle ( $ action )
{
if ( ! AdminUser::Logged) {
exit ( ' Please login first. ' );
}
$ action ();
}
}在某些情況下,您可能需要在拒絕路線操作後進行一些過程,只需將中間軟件邏輯放置在$action()呼叫語句之後即可。
use RouterOne MiddleWare RouteMiddleWareInterface ;
class AfterMiddleWare implements RouteMiddleWareInterface
{
public static function handle ( $ action )
{
$ action ();
echo ' This text will print after route action excuted. ' ;
}
}當定義中間軟件時,可以通過路由器的middleware()方法設置路由為grouped表單, middleware()具有兩個參數,第一個是middle-ware類名稱陣列和支持更多的中間Wares,另一個是封閉功能,包括通用路由映射。
$ this -> middleware (
[
AuthCheckMiddleWare::class,
...
. . .
], function () {
$ this -> get ( ' admin/index ' , [ Controllers Admin AdminController::class, ' index ' ]);
$ this -> get ( ' admin/news/list ' , [ Controllers Admin NewsController::class, ' list ' ]);
. . .
. . .
});也可以nested
$ this ->middleware(
[
OuterMiddleWare::class,
], function () {
. . .
$ this ->middleware([
InnerMiddleWare::class
], function () {
$ this ->get(...
$ this ->post(...
. . .
});
...
. . .
});prefix()和suffix()方法也是grouped路由,它們可以方便地添加實用的前綴和後綴到特定路由。
添加前綴static/ ,然後將URLS':// domain/static/page1','// domain/static/page2'匹配。
$ this -> prefix ( ' static/ ' , function () {
$ this -> get ( ' page1 ' , ...);
$ this -> get ( ' page2 ' , ...);
. . .
});添加the ,然後將URLS':// domain/thepage1','// domain/thepage2'匹配。
$ this -> prefix ( ' the ' , function () {
$ this -> get ( ' page1 ' , ...);
$ this -> get ( ' page2 ' , ...);
. . .
});與使用prefix()相同,添加後綴.html ,然後URL更改為'://domain/page1.html'。
$ this -> suffix ( ' .html ' , function () {
$ this -> get ( ' page1 ' , ...);
. . .
. . .
});在prefix()和suffix()之間可以彼此nested 。
$ this -> prefix ( ' static/ ' , function () {
$ this -> get ( ' page1 ' , ...); // request url '://domain/static/page1' matched here
. . .
$ this -> suffix ( ' .html ' , function () {
$ this -> get ( ' page2 ' , ...); // request url '://domain/static/page2.html' matched here
});
});如果您的應用程序具有多個URL域,則使用domain()方法可以區分這些域,並將請求引導到相應的路由組。
$ this -> domain ( ' www.hereisyoursite.com ' , function () { // PC Pages
$ this -> get ( ' index ' , ...);
. . .
});
$ this -> domain ( ' m.hereisyoursite.com ' , function () { // Mobile Pages
$ this -> get ( ' index ' , ...);
. . .
});
$ this -> domain ( ' new.hereisyoursite.com ' , function () { // Current Domain routes
$ this -> get ( ' index ' , ...);
. . .
});
$ this -> domain ( ' old.hereisyoursite.com ' , function () { // Legacy Domain routes
$ this -> get ( ' index ' , ...);
. . .
});