ไลบรารี URL การกำหนดเส้นทางเว็บแอปพลิเคชัน PHP ที่เป็นอิสระใช้งานง่าย
ดาวน์โหลดซอร์สโค้ดและตำแหน่งที่ถูกต้องของไดเรกทอรีโครงการของคุณ
รับอินสแตนซ์วัตถุเราเตอร์
use RouterOne Router ;
$ router = Router:: getInstance (); สร้างแผนที่เส้นทางในไฟล์เดียวและอยู่ใน prject ของคุณเฉพาะในไฟล์ $this อ้างถึงอินสแตนซ์วัตถุ $router คอนกรีต สำหรับคำกริยาร้องขอ HTTP, RouteOne สนับสนุนได้ GET เท่านั้น POST (จัดตำแหน่งกับ $ _get & $ _POST ของ PHP ซึ่งขยายออกไปทั้งหมดหากคุณต้องการหรือจำเป็น)
ตัวอย่างเช่น 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 paramter ตามลำดับของลักษณะที่ปรากฏ
$ 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 คุณสามารถค้นหาไฟล์คลาส Middle-Ware ในไดเรกทอรีโดยพลการเช่น MiddleWare DIR;
คลาสช่วงกลางแบบทั่วไปมีวิธีการ handle() ที่มีพารามิเตอร์การดำเนินการเส้นทาง- $action เช่นด้านล่าง:
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 และรองรับการดูแลกลางมากขึ้นที่นี่และอื่น ๆ เป็นฟังก์ชั่นปิดรวมถึงการแมปเส้นทางทั่วไป
$ 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 ' , ...);
. . .
}); เพิ่ม preifx 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 ' , ...);
. . .
});