koddn / php-router
Koddn PHP 路由器 - 为定制 PHP 应用、CMS、REST API(具有多个回调和中间件)创建的简单路由器
dev-main
2021-07-03 08:24 UTC
Requires
- php: >=7.4
This package is auto-updated.
Last update: 2024-09-29 05:43:14 UTC
README
Koddn Php 路由器是一个 PHP 类,可以轻松处理 PHP 中的应用程序的路线,支持多个回调、重定向,并以 JSON 格式向客户端发送响应。
文档链接: Koddn Php Router
社交媒体 Koddn: Facebook, Twitter, Instagram
开发者社交媒体: Facebook, Twitter, Instagram
功能
- 管理路由,get,post,put,delete,任何其他 - 其他
- 重定向页面
- 多个回调
- 下一个函数
- 匹配模式
- 发送响应,JSON,TEXT
- 设置状态头
- PHP 中的类似 Express 路由器
- PHP 中的快速路由
- 可作为 Auth 验证器使用
安装
使用 composer 安装
composer require koddn/php-router
用法
获取 Koddn Php Router
// 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"); });
用法 - 方法 2
只需将 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"); });
模式
您可以使用模式来匹配请求
- :paramName => 使用冒号开始的命名 URL 段,用于捕获在 URL 中指定位置的值
-
- => 将捕获任何内容
在下面的示例中,我们正在从 URL 中捕获用户 id
// 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 });
中间件
示例:如何将其用作身份验证中间件
中间件可以使用 "use" 方法实现
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"); });
发送 JSON 数据
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'); });
清除 cookie
ROUTER::post('/api/user', function($req,$res,$next){ // clear all cookies $res->clearCookies(); // clear specific cookies $res->clearCookies('nameOfCookie'); // do something });
清除 cookie
ROUTER::post('/api/user', function($req,$res,$next){ // clear Sessions $res->clearSession(); // do something });
结束请求
它与 die() 相似
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'); });