เราเตอร์ Koddn PHP เป็นคลาส PHP ที่สามารถจัดการเส้นทางในแอปพลิเคชัน PHP ได้อย่างง่ายดายรองรับการโทรกลับหลายครั้งเปลี่ยนเส้นทางและส่งการตอบกลับไปยังลูกค้าใน JSON
เว็บไซต์: Koddn Technologies
ลิงก์ไปยังเอกสาร: เราเตอร์ Koddn PHP
โซเชียลมีเดีย Koddn: Facebook, Twitter, Instagram
นักพัฒนาโซเชียลมีเดีย: Facebook, Twitter, Instagram
ในการติดตั้งด้วยนักแต่งเพลง:
composer require koddn/php-router
รับเราเตอร์ Koddn PHP
// composer auto loader
require __DIR__ . ' /vendor/autoload.php ' ;
use KODDN ROUTER ;
// match get request
ROUTER :: get ( ' / ' , function ( $ req , $ res , $ next ){
// ..do something
$ res -> send ( " Welcome to Koddn Php Router " );
});เพียงเพิ่มไฟล์ src/koddn_router.php ในโครงการของคุณ
// composer auto loader
require __DIR__ . ' /src/KODDN_ROUTER.php ' ;
// match get request
ROUTER :: get ( ' / ' , function ( $ req , $ res , $ next ){
// ..do something
$ res -> send ( " Thanks " );
});คุณสามารถใช้รูปแบบเพื่อให้ตรงกับคำขอ
ในตัวอย่างด้านล่างเรากำลังจับ ID ผู้ใช้จาก URL
// URL => /api/user/111
ROUTER :: get ( ' /api/user/:id ' , function ( $ req , $ res ){
$ id = $ req [ ' params ' ][ ' id ' ];
// $req['params]=>['id'='111']
});ในตัวอย่างที่เราจับโดยใช้
// URL => /flight/india-usa
ROUTER :: get ( ' /flight/:from-:to ' , function ( $ req , $ res ){
// $req['params]=['from'=>'india','to'=>'usa'];
$ from = $ req [ ' params ' ][ ' from ' ];
$ to = $ req [ ' params ' ][ ' to ' ];
}); // URL => /post-name-apc/123
ROUTER :: get ( ' /*/:postID ' , function ( $ req , $ res ){
// $req['params']=['postID'=>'123'];
$ postID = $ req [ ' params ' ][ ' postID ' ];
// do something
});เราสามารถใช้ฟังก์ชั่นการโทรกลับหลายรายการเพื่อจัดการเส้นทาง
ROUTER :: get ( ' /some-url ' , function ( $ req , $ res , $ next ){
// Do something here
echo " START " ;
// call Next Callback, control goes to next callback function
$ next ();
}, function ( $ req , $ res ){
echo " END " ; // task completed
});นอกจากนี้หากเราต้องการให้มีการอ้างอิงคำขอในการโทรกลับถัดไปให้ใช้ & $ req เป็นพารามิเตอร์คำขอ
ROUTER :: get ( ' /dashboard ' , function (& $ req , $ res , $ next ){
// if user authorized
$ req [ ' userID ' ]= " someUserID " ;
$ next ();
}, function (& $ req ){
// now you can have use the req.userID here as well
});ตัวอย่างวิธีที่เราสามารถใช้เป็นมิดเดิลแวร์เพื่อการรับรองความถูกต้องได้อย่างไร
Middlewares สามารถใช้งานได้โดยใช้วิธี "ใช้"
ROUTER :: use ( ' /user ' , function ( $ req , $ res , $ next ){
ROUTER :: get ( ' /me ' , function ( $ req , $ res , $ next ) {
// do something here
});
});
// OR
ROUTER :: use ( ' /user ' , function ( $ req , $ res , $ next ){
require __DIR__ . " /routes/user.php " ; //
}); ROUTER :: post ( ' /login ' , function (& $ req , $ res , $ next ){
// do the authorize stuff here
if (!authorize){
$ res -> send ( " invalid " );
}
$ next ();
}, function (& $ req , $ res ){
// do something if authorized
// grantAccessToSomething
}); //ROUTER::redirect('/url-to-match', callbackBeforeRedirect, 'redirect to url', $replaceHeaders=false (optional), $redirectCode =301 (optional));
ROUTER :: redirect ( ' /url-to-match ' , function (){ /*do some logs*/ }, ' /new-url ' , $ replaceHeaders = false /*( boolean optional)*/ , $ redirectCode = 301 /*(int optional)*/ );ด้วยเราเตอร์ Koddn PHP ไม่ว่าคุณจะสามารถจัดการคำตอบด้วยตนเองหรือคุณสามารถใช้สิ่งที่อยู่ในตัวได้
ROUTER :: post ( ' /about ' , function ( $ req , $ res , $ next ){
$ res -> send ( " About us " );
}); ROUTER :: post ( ' /api/user ' , function ( $ req , $ res , $ next ){
$ userData =[ ' name ' => " Harpal Singh " , ' id ' => 11 ];
$ res -> json ( $ userData );
}); ROUTER :: post ( ' /api/user ' , function ( $ req , $ res , $ next ){
$ res -> setStatus ( 404 )-> send ( ' Not Found ' );
}); ROUTER :: post ( ' /api/user ' , function ( $ req , $ res , $ next ){
// clear all cookies
$ res -> clearCookies ();
// clear specific cookies
$ res -> clearCookies ( ' nameOfCookie ' );
// do something
}); ROUTER :: post ( ' /api/user ' , function ( $ req , $ res , $ next ){
// clear Sessions
$ res -> clearSession ();
// do something
});มันคล้ายกับตาย ();
ROUTER :: post ( ' /api/user ' , function ( $ req , $ res , $ next ){
// clear Sessions
$ res -> end ();
// do something
}); //ROUTER::redirect('redirect to url', $replaceHeaders=false (optional), $redirectCode =301 (optional));
ROUTER :: post ( ' /api/user ' , function ( $ req , $ res , $ next ){
// clear Sessions
$ res -> redirect ( ' /new-url ' , $ replaceHeaders = false /*(optional)*/ , $ redirectCode = 301 /*(optional)*/ );
// do something
}); ROUTER :: any ( ' /url-to-match ' , function (& $ req , $ res , $ next ){} /*, function(&$req,$res,$next){}*/ );
ROUTER :: post ( ' /url-to-match ' , function (& $ req , $ res , $ next ){} /*, function(&$req,$res,$next){}*/ );
ROUTER :: get ( ' /url-to-match ' , function (& $ req , $ res , $ next ){} /*, function(&$req,$res,$next){}*/ );
ROUTER :: put ( ' /url-to-match ' , function (& $ req , $ res , $ next ){} /*, function(&$req,$res,$next){}*/ );
ROUTER :: delete ( ' /url-to-match ' , function (& $ req , $ res , $ next ){} /*, function(&$req,$res,$next){}*/ );
ROUTER :: redirect ( ' /url-to-match ' , function (){ /*do some logs*/ }, ' /new-url ' , $ replaceHeaders = false /*( boolean optional)*/ , $ redirectCode = 301 /*(int optional)*/ ); ROUTER :: post ( ' /url-to-match ' , function (& $ req , $ res , $ next ){
//
//$req = ['$fullUrl' => $fullUrl, 'url' => $url, 'path' => $path, 'params' => $params, 'rPath' => $rPath];
}); ROUTER :: post ( ' /url-to-match ' , function (& $ req , $ res , $ next ){
//send
$ res -> send ( ' Some Text ' );
//json
$ res -> json ([ ' id ' => 11 , ' name ' => ' Harpal Singh ' ]);
//redirect using response
$ res -> redirect ( ' /new-url ' , $ replaceHeaders = false /*(optional)*/ , $ redirectCode = 301 /*(optional)*/ );
// Exit
$ res -> end ();
// clear all cookies
$ res -> clearCookies ();
// clear specific cookies
$ res -> clearCookies ();
//set Status
$ res -> setStatus ( 404 )-> send ( ' Not Found ' );
});